shouldOverrideUrlLoading自定义Cordova CordovaWebViewClient不再工作

时间:2014-10-03 13:49:28

标签: android cordova webview

我有一个自定义类来覆盖CordovaWebViewClient提供的方法shouldOverrideUrlLoading。

    public class CordovaCustomWebClient extends CordovaWebViewClient {

        public CordovaCustomWebClient(CordovaInterface cordova, CordovaWebView view) {
            super(cordova, view);
        }

        @SuppressLint("DefaultLocale")
        @SuppressWarnings("deprecation")
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            EventLogger.logMessage(getClass(), "--------------- shouldOverrideUrlLoading ---------------");
       return super.shouldOverrideUrlLoading(view, url);
       }

在我升级到最新版本的Cordova(3.6.3)之前,它工作正常。现在不再调用函数shouldOverrideUrlLoading,但是当我调试代码时,我可以看到在Cordova库中执行相同的函数(类CordovaWebViewClient)。

以下是我为了覆盖Cordova的网络客户端所做的工作:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_application);

        cordovaWebView = (CordovaWebView) this.findViewById(R.id.mainView);

        Config.init(this);

        Application application = null;
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            application = (Application) bundle.get("key_application");
        }

        // Local Url to load application
        String url = "";
        if (application != null) {
            if (HubManagerHelper.getInstance().getApplicationHosted() == null) {
                MyApp app = (MyApp) getApplication();
                app.registerDefaultHubApplication();
            }
            url = String.format(WebServicesClient.URL_WEB_APPLICATION, HubManagerHelper.getInstance()
                    .getApplicationHosted(), application.getPath());
        }

        cordovaWebView.setWebViewClient(new CordovaCustomWebClient(this, cordovaWebView));

        // Listener to Download Web File with Native Component - Download Manager
        cordovaWebView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,
                    long contentLength) {
                downloadAndOpenFile(WebApplicationActivity.this, url);
            }
        });

        String ua = cordovaWebView.getSettings().getUserAgentString();
        String appVersion = getAppVersion();
        String newUA = ua.concat(" MyApp." + appVersion);
        cordovaWebView.getSettings().setUserAgentString(newUA);
        if (savedInstanceState == null) {
            cordovaWebView.loadUrl(url);
        } else {
            ((LinearLayout) findViewById(R.id.view_loading)).setVisibility(View.GONE);
        }

1 个答案:

答案 0 :(得分:4)

升级到3.6.3后,我今天遇到了同样的问题。我们花了很多时间来查看Cordova源代码。在某些时候,引入了一个新方法init,它从config.xml中读取了一堆参数。如果您的代码没有调用该方法,那么当加载网址时,它会触及initIfNecessary大小写,而后者将覆盖已设置的所有自定义客户端。

从他们的代码:

private void initIfNecessary() {
    if (pluginManager == null) {
        Log.w(TAG, "CordovaWebView.init() was not called. This will soon be required.");
        // Before the refactor to a two-phase init, the Context needed to implement CordovaInterface. 
        CordovaInterface cdv = (CordovaInterface)getContext();
        if (!Config.isInitialized()) {
            Config.init(cdv.getActivity());
        }
        init(cdv, makeWebViewClient(cdv), makeWebChromeClient(cdv), Config.getPluginEntries(), Config.getWhitelist(), Config.getExternalWhitelist(), Config.getPreferences());
    }
}

您可以看到makeWebViewClient被调用,即使您可能已经设置了自己的客户端。

我用以下方法解决了这个问题:

ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(activity);

CordovaInterface cordova = (CordovaInterface) activity;
init(cordova, new WFWebViewClient(cordova, this), makeWebChromeClient(cordova),
    parser.getPluginEntries(), parser.getInternalWhitelist(), parser.getExternalWhitelist(),
    parser.getPreferences());

并删除了Config.init(activity);

的弃用用法

希望这可以节省你今天浪费的时间。