最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Closing a Webview in Android - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

Why 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.

发布评论

评论列表(0)

  1. 暂无评论