简体   繁体   English

在Android中关闭Webview

[英]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. 当用户登录页面时,代码使用Javascript函数对网页进行解析,以表示“ A”,“ I”或“ N”,从而为用户提供了对网页其他功能的不同访问权限。

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. 问题是登录后似乎无法立即关闭WebView 。目前,我已经实现了closeWebview按钮,用户登录后可以单击该按钮将关闭页面,但是当我尝试时在身份验证后自动关闭它似乎无法正常工作。 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: 并且,仅供参考,这是我的closeWebview按钮的代码(包含在onClick(View v)方法中:

    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:- 我建议您检查URL字符串以获取特定的成功URL字符串,即Amazon登录后:

https://www.amazon.co.uk/gp/yourstore/home?ie=UTF8&ref_=gno_signin& 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. 您必须先登录,然后才能转到该URL。登录后找出URL中包含的内容,然后执行类似操作。

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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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