如何与QUOT;显示器和QUOT;在WebView中的URL基地 [英] How to "Monitor"the URL base in WebView

查看:174
本文介绍了如何与QUOT;显示器和QUOT;在WebView中的URL基地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我希望能够监控我的WebView加载应用程序的URL。最主要的我尝试做到的是保持的WebView内特定的网站或至少能够当用户离开网站的URL检测。

So i want to be able to monitor the URL my WebView application is loading. The main thing im trying to accomplish is to keep the WebView inside a specific Website or at least by able to detect when the user is leaving the website's URL.

为例。如果我有www.google.com,当用户点击图片,他得到 HTTP基本网址:// WWW .google.com / imghp 不过它还是有www.google.com的基本URL,所以如果有监控时,基本URL不再www.google.com的方式,例如当用户点击一个搜索结果,然后带到一个不同的网站完全。

Example. If i have the base URL of www.google.com and when the user clicks Images, he gets "http://www.google.com/imghp" However it still has the base URL of www.google.com, so if there is a way to monitor when the base URL is no longer www.google.com, such as when a user clicks a search result and is then brought to a different website completely.

这样做的目的是,我想向用户显示一个对话框或吐司,让他们知道他们离开这的WebView应用为其创建的网站。

The purpose of doing this is that i want to display to the user a dialog or Toast to let them know they are leaving the website that this WebView app is created for.

任意code,IDEA的或建议将不胜AP preciated。

Any Code, Idea's or suggestions would be greatly appreciated.

举例code

 @Override
 public void onLoadResource(WebView view, String url)
    {
        if (url.equals("http://www.google.com"))
        {
            //do your own thing here
            view.loadUrl(url);
        }
        else
        {
            AlertDialog.Builder localBuilder2 = new AlertDialog.Builder(Webview_Main.this);
            localBuilder2.setTitle("Hey Wait!");
            localBuilder2.setMessage("You currently are about to leave the website\n\nIf you want to stay inside this website, Please click the Go Back button below");
            localBuilder2.setIcon(R.drawable.ic_launcher);
            localBuilder2.setPositiveButton("Go Back",
                    new DialogInterface.OnClickListener() {
                public void onClick(
                        DialogInterface paramDialogInterface,
                        int paramInt) {
                    web.goBack();
                }
            });
            localBuilder2.setNegativeButton("Continue",
                    new DialogInterface.OnClickListener() {
                public void onClick(
                        DialogInterface paramDialogInterface,
                        int paramInt) {

                }
            });

            localBuilder2.show();
        };
            super.onLoadResource(view, url);
        }    

然而,这上面code不工作,只保留坡平了,因为URL可能不同的警告对话框。

However this above code doesnt work, only keeps poping up the alert dialog since the URL maybe different.

推荐答案

我已经解决了我的问题有以下code。

I have resolved my issue with the following code.

 @Override
 public boolean shouldOverrideUrlLoading(WebView view, String url) {
           if (url.startsWith("http://www.google.com"))
            { view.loadUrl(url);
            progressBar.setVisibility(View.VISIBLE);
            }
          else
            { Toast.makeText(getBaseContext(),
            "Your about to leave Google.com",
            Toast.LENGTH_LONG).show();
    }
 }

由于@MH。您的建议

Thanks @MH. for the suggestion

这篇关于如何与QUOT;显示器和QUOT;在WebView中的URL基地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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