【问题标题】:shouldOverrideUrlLoading on custom Cordova CordovaWebViewClient not working anymore自定义 Cordova CordovaWebViewClient 上的 shouldOverrideUrlLoading 不再工作
【发布时间】:2014-10-03 13:49:27
【问题描述】:

我有一个自定义类来覆盖由 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);
        }

【问题讨论】:

    标签: android cordova webview


    【解决方案1】:

    升级到 3.6.3 后,我今天遇到了同样的问题。它需要查看 Cordova 源代码才能弄清楚为什么会被破坏。在某个时候,引入了一种新方法init,它需要从您的 config.xml 中读取一堆参数。如果您的代码未调用该方法,则在加载 url 时,它将遇到 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);

    希望这可以为您节省一些我今天浪费的时间。

    【讨论】:

    • 谢谢托德!我实际上做了一些不同的事情,但问题正是那个。我创建了自己的自定义方法 makeWebViewClient ,它检查那里是否已经有一个,如果有,它不会替换它。在我的解决方案中。 @Override public CordovaWebViewClient makeWebViewClient(CordovaInterface cordova) { if(this.webViewClient == null) { webViewClient = super.makeWebViewClient(cordova); } 返回 this.webViewClient; }
    • 我实际上更喜欢你的解决方案,并且几乎重构了我的解决方案以覆盖 makeWebViewClient,但 initIfNecessary 中关于需要 init 的评论很快让我害怕只进行不必要的复杂 init 调用.我不确定为什么我们必须自己进行配置解析。
    • @ToddT 你能详细说明你把你的代码块放在哪里吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2016-08-13
    相关资源
    最近更新 更多