java - Closing a Webview in Android - Stack Overflow

Basically, I want to load a webpage, wait for the user to log in and then automatically close the webpa

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745449348a4628195.html

相关推荐

  • java - Closing a Webview in Android - Stack Overflow

    Basically, I want to load a webpage, wait for the user to log in and then automatically close the webpa

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信