Android WebView从WebViewClient到Activity通信

我有一个简单的helloworld应用程序.我试图告诉活动用户单击了“ Cnn.com”

    WebViewClient wc = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view,String url) {
            if (url.equals("http://cnn.com/")) {
                //TELL ACTIVITY CNN WAS CLICKED
                return true;
            } else {
                return false;

            }
        }

    };

    mWebView.setWebViewClient(wc);

我该怎么做.

(我来自C#.NET背景)

最佳答案
public class YourActivity extends Activity{

    // bla bla bla

    // the code you already have
    WebViewClient wc = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view,String url) {
            if (url.equals("http://cnn.com/")) {
                YourActivity.this.tellMe();
                return true;
            } else {
                return false;

            }
        }

    };

    // this is the method to 'tell' the activity that someone clicked the link
    public void tellMe(){
        // in this case I just raise a toast.
        // but you can do whatever you want here ;)
        Toast.makeText(YourActivity.this,"eyy!! somebody clicked the cnn link",1).show();
    }
}

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...