2014-10-03 20 views
4

我有一个自定义类来覆盖由CordovaWebViewClient提供的方法shouldOverrideUrlLoading。shouldOverrideUrl加载自定义科尔多瓦CordovaWebViewClient不工作了

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

这是工作的罚款,直到我已经升级到科尔多瓦的最新版本(3.6.3)。现在,函数shouldOverrideUrlLoading不再被调用,但是当我调试代码时,我可以看到在Cordova库(类CordovaWebViewClient)中执行的相同函数。

这是我做的重写科尔多瓦的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); 
     } 

回答

4

我今天打这个相同问题升级到3.6.3之后。它考虑了科尔多瓦源代码来弄清为什么这被打破。在某个时候,引入了一种新方法,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);

希望这可以节省你一些我今天已经浪费的时间。

+0

谢谢托德!我其实做了一些不同的事情,但问题恰恰在于那个。我创建了自己的自定义方法makeWebViewClient,它检查是否已经有一个,如果是的话,它不会取代它。在我的情况下解决它。 @Override 公共CordovaWebViewClient makeWebViewClient(CordovaInterface科尔多瓦){ \t如果(this.webViewClient == NULL) \t { \t \t webViewClient = super.makeWebViewClient(科尔多瓦); \t} \t回报this.webViewClient; } – Marco 2014-10-06 10:41:39

+0

我实际上更喜欢你的解决方案,并且几乎重构了我的方法来覆盖'makeWebViewClient',但initIfNecessary中有关初始化的注释很快就让我不敢回想起进行不必要的复杂init调用。我不知道为什么我们不得不自己配置解析。 – 2014-10-06 13:21:32

+0

@ToddT你能否详细说明你把你的代码块放在哪里? – mentat 2014-10-29 08:58:01