如何拦截cordova中的网址加载

时间:2014-11-13 10:43:58

标签: android cordova

我正在尝试拦截cordova活动中的url加载,就像我在普通的Android应用程序中一样,但是我在初始化CordovaWebViewClient时遇到了麻烦!

这是我的代码:

// In my CordovaActivity's onCreate function:
appView.setWebViewClient(new CordovaWebViewClient( this) {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
          if (loadUrlExternally){
              Uri uri = Uri.parse(url);
              Intent intent = new Intent(Intent.ACTION_VIEW, uri);
              startActivity(intent);
              return true; //the webview will not load the URL
          } else {
              return false; //the webview will handle it
          }
    }
}

1 个答案:

答案 0 :(得分:2)

我找到了解决方案,我没有正确初始化CordovaWebViewClient。 以下是我的代码现在的样子:

// Load external urls in browser
    appView.setWebViewClient(new CordovaWebViewClient(this, appView) {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if (!url.contains(AppConfig.BASE_URL)){
                  Uri uri = Uri.parse(url);
                  Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                  startActivity(intent);
                  return true; //the webview will not load the URL
              } else {
                  return false; //the webview will handle it
              }
        }
    });