admin管理员组文章数量:1425819
Basically, I want to load a webpage, wait for the user to log in and then automatically close the webpage and continue with the application. When the user is logged in on the page, the code parses the webpage using a Javascript function for an "A", an "I", or an "N," which gives the user varying levels of access to other features of the webpage.
The problem is that I can't seem to get it to close the WebView
immediately after logging in. Currently, I have implemented a closeWebview
button that the user can click after they've logged in that will close the page, but when I try to close it automatically after authentication I can't get it to seem to work. It either closes immediately, or I can't close it at all.
Here is my code for Authentication:
private void Authentication(){
class MyJavaScriptInterface {
@JavascriptInterface
public void showHTML(String content) {
// grants access based on authorization level
if (content.contains("A")) {
addAuthentication = true;
inquireAuthentication = true;
loggedIn = true;
Toast.makeText(getApplicationContext(), "Logged In for Addition and Inquiry", Toast.LENGTH_SHORT).show();
}else if(content.contains("I")){
addAuthentication = false;
inquireAuthentication = true;
loggedIn = true;
Toast.makeText(getApplicationContext(), "Logged In for Inquiry only", Toast.LENGTH_SHORT).show();
}else {
addAuthentication = false;
inquireAuthentication = false;
loggedIn = true;
Toast.makeText(getApplicationContext(), "No Access Granted", Toast.LENGTH_SHORT).show();
}
}
}
// open up the login page
final WebView wv = (WebView)findViewById(R.id.login_webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
wv.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
//once page is finished loading, check id="role" and copy that value and pass it to showHTML
wv.loadUrl("javascript:(function() { " +
"window.HTMLOUT.showHTML(document.getElementById('role').innerHTML);" +
"})()");
}
@Override
public void onReceivedError(WebView view, int errorCode, String description,
String failingUrl) {
Log.w("LoginActivity: ", description);
}
});
wv.loadUrl(getString(R.string.loginURL));
if(!loggedIn) {
wv.setVisibility(View.VISIBLE);
closeWebview.setVisibility(View.VISIBLE);
}else{
closeWebview.setVisibility(View.GONE);
wv.setVisibility(View.GONE);
}
}
And, just for reference, here is the code for my closeWebview
button (contained in the onClick(View v)
method:
case R.id.close_webview:
// closes the login webpage
WebView wv = (WebView) findViewById(R.id.login_webview);
wv.setVisibility(View.GONE);
closeWebview.setVisibility(View.GONE);
break;
Basically, I want to load a webpage, wait for the user to log in and then automatically close the webpage and continue with the application. When the user is logged in on the page, the code parses the webpage using a Javascript function for an "A", an "I", or an "N," which gives the user varying levels of access to other features of the webpage.
The problem is that I can't seem to get it to close the WebView
immediately after logging in. Currently, I have implemented a closeWebview
button that the user can click after they've logged in that will close the page, but when I try to close it automatically after authentication I can't get it to seem to work. It either closes immediately, or I can't close it at all.
Here is my code for Authentication:
private void Authentication(){
class MyJavaScriptInterface {
@JavascriptInterface
public void showHTML(String content) {
// grants access based on authorization level
if (content.contains("A")) {
addAuthentication = true;
inquireAuthentication = true;
loggedIn = true;
Toast.makeText(getApplicationContext(), "Logged In for Addition and Inquiry", Toast.LENGTH_SHORT).show();
}else if(content.contains("I")){
addAuthentication = false;
inquireAuthentication = true;
loggedIn = true;
Toast.makeText(getApplicationContext(), "Logged In for Inquiry only", Toast.LENGTH_SHORT).show();
}else {
addAuthentication = false;
inquireAuthentication = false;
loggedIn = true;
Toast.makeText(getApplicationContext(), "No Access Granted", Toast.LENGTH_SHORT).show();
}
}
}
// open up the login page
final WebView wv = (WebView)findViewById(R.id.login_webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
wv.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
//once page is finished loading, check id="role" and copy that value and pass it to showHTML
wv.loadUrl("javascript:(function() { " +
"window.HTMLOUT.showHTML(document.getElementById('role').innerHTML);" +
"})()");
}
@Override
public void onReceivedError(WebView view, int errorCode, String description,
String failingUrl) {
Log.w("LoginActivity: ", description);
}
});
wv.loadUrl(getString(R.string.loginURL));
if(!loggedIn) {
wv.setVisibility(View.VISIBLE);
closeWebview.setVisibility(View.VISIBLE);
}else{
closeWebview.setVisibility(View.GONE);
wv.setVisibility(View.GONE);
}
}
And, just for reference, here is the code for my closeWebview
button (contained in the onClick(View v)
method:
case R.id.close_webview:
// closes the login webpage
WebView wv = (WebView) findViewById(R.id.login_webview);
wv.setVisibility(View.GONE);
closeWebview.setVisibility(View.GONE);
break;
Share
Improve this question
asked Jun 11, 2014 at 14:24
jhobbiejhobbie
1,0169 silver badges18 bronze badges
1 Answer
Reset to default 5Why not start a new activity once authenticated or inflate a fragment? What I would remend is check the url string for a specific success url string i.e Amazon once signed in:-
https://www.amazon.co.uk/gp/yourstore/home?ie=UTF8&ref_=gno_signin&
you cannot go to this url unless you are signed in. Find out what the url contains once signed in and do something like.
public void onPageFinished(WebView view, String url) {
if (url.contains("https://www.amazon.co.uk/gp/yourstore/home"))
{
// Successfully logged in, load your logged in activity
startActivity(new Intent(this, **yournewclass**.class));
}
}
Hope this helps.
本文标签: javaClosing a Webview in AndroidStack Overflow
版权声明:本文标题:java - Closing a Webview in Android - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745449348a2658811.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论