关闭在Android的网页视图 [英] Closing a Webview in Android

查看:167
本文介绍了关闭在Android的网页视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想加载一个网页,等待用户登录,然后自动关闭网页,并继续与应用程序。当用户登录的页面上,在code解析使用JavaScript函数为A的我,或者这给用户不同的接入其他功能水平的网页,N的网页。

问题是,我似乎无法得到它在登录后立即关闭的WebView。目前,我已经实现了一个 closeWebview 按钮,他们已经记录在之后将关闭该页面,但是当我尝试验证后自动关闭它,我不能让它似乎工作的用户可以点击。它要么立即关闭,否则我无法关闭它。

下面是我的code为验证:

 私人无效认证(){    类MyJavaScriptInterface {        @JavascriptInterface
        公共无效showHTML(字符串内容){
            //允许访问基于授权级别
            如果(content.contains(A)){
                addAuthentication = TRUE;
                inquireAuthentication = TRUE;
                的loggedIn = TRUE;                Toast.makeText(getApplicationContext()登录的加法和探究,Toast.LENGTH_SHORT).show();
            }否则如果(content.contains(I)){
                addAuthentication = FALSE;
                inquireAuthentication = TRUE;
                的loggedIn = TRUE;                Toast.makeText(getApplicationContext()中记录只探究,Toast.LENGTH_SHORT).show();
            }其他{
                addAuthentication = FALSE;
                inquireAuthentication = FALSE;
                的loggedIn = TRUE;                Toast.makeText(getApplicationContext(),无授权访问,Toast.LENGTH_SHORT).show();
            }
        }
    }    //打开登录页面
    最终的WebView WV =(的WebView)findViewById(R.id.login_webview);    wv.getSettings()setJavaScriptEnabled(真)。    wv.addJavascriptInterface(新MyJavaScriptInterface(),HTMLOUT);    wv.setWebViewClient(新WebViewClient(){
        @覆盖
        公共无效onPageFinished(的WebView视图,字符串URL){            //一旦页面加载完毕后,检查ID =角色,并复制值,并将其传递给showHTML            wv.loadUrl(JavaScript的:(函数(){+
                    window.HTMLOUT.showHTML(的document.getElementById('角色')的innerHTML。); +
                    })());        }        @覆盖
        公共无效onReceivedError(的WebView观点,诠释错误code,字符串描述,
                                    字符串failingUrl){
            Log.w(LoginActivity:,说明);
        }
    });    wv.loadUrl(的getString(R.string.loginURL));
    如果(!的loggedIn){
        wv.setVisibility(View.VISIBLE);
        closeWebview.setVisibility(View.VISIBLE);
    }其他{
        closeWebview.setVisibility(View.GONE);
        wv.setVisibility(View.GONE);
    }
}

和,仅供参考,这里是code为我的 closeWebview 按钮(包含在的onClick(视图v)方法:

 情况下R.id.close_webview:
            //关闭登录网页
            WV的WebView =(的WebView)findViewById(R.id.login_webview);
            wv.setVisibility(View.GONE);
            closeWebview.setVisibility(View.GONE);
            打破;


解决方案

为什么不启动一次验证了新的活动或膨胀的片段?我建议是检查URL字符串为特定的成功URL字符串即亚马逊曾经在签署: -

https://www.amazon.co.uk/gp/yourstore/home?ie=UTF8&ref_=gno_signin&

你不能去这个网址,除非您已登录。找出URL包含在一旦签署,并完成类似。

 公共无效onPageFinished(的WebView视图,字符串URL){    如果(url.contains(https://www.amazon.co.uk/gp/yourstore/home))
    {
        //成功登录后,加载在活动记录
        startActivity(新意图(这一点,** ** yournewclass类));
    }}

希望这有助于。

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

这篇关于关闭在Android的网页视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆