Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是否应在自定义Cordova CordovaWebViewClient上重写URL加载不再工作_Android_Cordova_Webview - Fatal编程技术网

Android 是否应在自定义Cordova CordovaWebViewClient上重写URL加载不再工作

Android 是否应在自定义Cordova CordovaWebViewClient上重写URL加载不再工作,android,cordova,webview,Android,Cordova,Webview,我有一个自定义类来重写CordovaWebViewClient提供的shouldOverrideUrlLoading方法 public class CordovaCustomWebClient extends CordovaWebViewClient { public CordovaCustomWebClient(CordovaInterface cordova, CordovaWebView view) { super(cordova, vie

我有一个自定义类来重写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的web客户端:

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);
        }

今天升级到3.6.3后,我遇到了完全相同的问题。需要查看Cordova的源代码才能弄清楚为什么会出现这种情况。在某个时候,引入了一个新方法,
init
,它从config.xml读取一组参数。如果您的代码未调用该方法,则加载url时,它将命中
initifneeded
案例,这反过来将覆盖已设置的任何自定义客户端

根据他们的代码:

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(活动)


希望这能帮你省下我今天浪费的时间。

谢谢托德!我确实做了一些不同的事情,但问题恰恰是那个。我已经创建了自己的自定义方法makeWebViewClient,它检查是否已经有一个,如果已经有,它将不会替换它。在我的场景中,它解决了这个问题@重写公共CordovaWebViewClient makeWebViewClient(CordovaInterface cordova){如果(this.webViewClient==null){webViewClient=super.makeWebViewClient(cordova);}返回this.webViewClient;}我实际上更喜欢您的解决方案,并且几乎重构了我的解决方案来重写
makeWebViewClient
,但是
initifnequired
中关于需要init的评论很快让我害怕,只进行了不必要的复杂init调用。我不知道为什么我们必须自己进行配置解析。@ToddT你能详细说明一下你的代码块放在哪里吗?