简体   繁体   中英

Closing a Webview in Android

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;

Why not start a new activity once authenticated or inflate a fragment? What I would recommend is check the url string for a specific success url string ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM