我该如何与QUOT;听"在WebView中,Android的东西吗? [英] How do I "listen" for something in WebView, Android?

查看:123
本文介绍了我该如何与QUOT;听"在WebView中,Android的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }   }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        webview = (WebView) findViewById(R.id.webview);
        webview.setWebViewClient(new HelloWebViewClient());
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("http://mydomain.com");
    }

这是一个非常简单的WebView演示(我也跟着教程写)。当用户加载了我的应用程序,该web视图弹出,他能够继续在互联网上里面。

This is a very simple webview demo ( I followed a tutorial to write it). When a user loads up my application, this webview pops up and he's able to go on the internet inside it.

我如何倾听的事件?


  • 当URL包含google.com

  • 或者,当HTML包含单词谷歌

由于用户在使用我的web视图来浏览网页,我想听听这些事情,然后调用一个函数,当它发生。

As the user is using my webview to browse the web, I'd like to listen to those things and then call a function when it happens.

推荐答案

shouldOverrideUrlLoading的文档,我花了一对夫妇阅读理解。

The documentation of shouldOverrideUrlLoading took me a couple readings to understand.

如果你想在当前的WebView来处理URL那么就没有必要调用WebView.loadUrl,刚刚返回false。如果你希望你的应用程序做一些特别的URL完全不同的然后做你需要什么,并返回true。

If you want the current WebView to handle the url then there is no need to call WebView.loadUrl, just return false. If you want your app to do something completely different with particular URLs then do what you need and return true.

我希望那些没有从我的主机由手机浏览器的应用程序,而不是我的WebView处理所有URL。下面是我实现的:

I wanted all URLs that were not from my host to be handled by the phone's browser app rather than my WebView. Here is my implementation:

 @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    boolean returnVal = false;
    if(url.startsWith(mUrlHost)) {
      //current Webview handles the URL
    } else {
      Toast.makeText(mActivity, "Launching Browser", Toast.LENGTH_SHORT).show();
      Uri uri = Uri.parse(url);
      Intent intent = new Intent(Intent.ACTION_VIEW, uri);
      mActivity.startActivity(intent);
      returnVal = true;
    }
    return returnVal;
  }

这篇关于我该如何与QUOT;听"在WebView中,Android的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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