2014-10-22 74 views
0

我有一個應用程序,可以在所有Api> 11包括Kitkat 4.4.2上正常工作。Complient鏈接URL不調用onPageFinished在WebView KitKat 4.4.4

然而,在奇巧4.4.4 onPageFinished()不燒成1條鏈路:

HREF = 「http://app.host.com/projects/30/edit/」

我知道KitKat WebView的問題,並從我知道這個網址應該工作。

與此URL對應的網絡應用程序是用RoR編寫的,並禁用了turbolinks

我的WebView客戶端如下所示。

我花了好幾天的時間試圖讓這個工作。

如果有人發現任何錯誤,請告訴我。

// Set WebView client 
     mWebAppView.setWebViewClient(new WebViewClient() { 
      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 

       Log.d(TAG, "#shouldOverrideUrlLoading url called: " + url); 

       // Open an email client on device when openning a mailto: link 
       if (MailTo.isMailTo(url)) { 
        Intent intent = null; 
        try { 
         intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); 
        } catch (URISyntaxException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        view.getContext().startActivity(intent); 
        return true; 
        // open the help link in a browser. 
       } else if (url 
         .contains("www.host.com/expertise/business-development/index.cfm")) { 
        Log.d(TAG, "should open browser"); 
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); 
        return true; 
       } else { 
        view.loadUrl(url); 
        return true; 
       } 

      } 

      @SuppressLint("InlinedApi") 
      @Override 
      public void onPageFinished(WebView view, String url) { 
//    super.onPageFinished(view, url); 
       view.clearCache(true); 
       Log.d(TAG, "#onPageFinished loaded page is :"+url); 
       if (pd.isShowing() && pd != null) { 
        pd.dismiss(); 
        Log.d(TAG, "Inside #onPageFinished, pd dismissed"); 
       } 

       // Change orientation for the table screen for phones so that 
       // the tables show correctly 
       if ((url.contains("projects")) 
         && ((url.contains("stepfirst")) 
           || (url.contains("stepsecond")) || (url 
            .contains("edit"))) && !isTablet) { 
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
       } else if (!isTablet) { 
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
       } 
      } 

      @Override 
      public void onLoadResource(WebView view, String url) { 
       // TODO Auto-generated method stub 
       super.onLoadResource(view, url); 
       Log.d(TAG, "Inside #onLoadResource url :"+url); 
      } 

      @Override 
      public void onPageStarted(WebView view, String url, Bitmap favicon) { 
       // TODO Auto-generated method stub 
       super.onPageStarted(view, url, favicon); 
       Log.d(TAG, "Inside #onPageStarted url :"+url); 
      } 

      @Override 
      public WebResourceResponse shouldInterceptRequest(WebView view, 
        String url) { 
       // TODO Auto-generated method stub 
       Log.d(TAG, "Inside #shouldInterceptRequest url :"+url); 
       return super.shouldInterceptRequest(view, url); 

      } 



     }); 

回答

0

所以,這是一個醜陋的黑客攻擊,我決定用:

雖然onPageFinished(),onPageStart()和shouldOverrideUrlLoading()並不總是由URL的調用,即使格式正確無誤。

我發現shouldInterceptRequest()被所有無法比擬的url所激發,我試過了(只有有限且不完全科學),它給了我相同的數據來通過url控制流,雖然在頁面加載的早期階段。因此,我使用shouldInterceptRequest()而不是非常有限的onPageFinished(),onPageStart()和shouldOverrideUrlLoading()。

這裏是代碼:

// Set WebView client 
    mWebAppView.setWebViewClient(new WebViewClient() { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 

      Log.d(TAG, "#shouldOverrideUrlLoading url called: " + url); 

      // Open an email client on device when openning a mailto: link 
      if (MailTo.isMailTo(url)) { 
       Intent intent = null; 
       try { 
        intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); 
       } catch (URISyntaxException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       view.getContext().startActivity(intent); 
       return true; 
       // open the help link in a browser. 
      } else if (url 
        .contains("www.host.com/expertise/business-development/index.cfm")) { 
       Log.d(TAG, "should open browser"); 
       startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); 
       return true; 
      } else { 
       view.loadUrl(url); 
       return true; 
      } 

     } 

     @SuppressLint("InlinedApi") 
     @Override 
     public void onPageFinished(WebView view, String url) { 
      // super.onPageFinished(view, url); 
      view.clearCache(true); 
      Log.d(TAG, "#onPageFinished loaded page is :" + url); 
      if (pd.isShowing() && pd != null) { 
       pd.dismiss(); 
       Log.d(TAG, "Inside #onPageFinished, pd dismissed"); 
      } 

      // Change orientation for the table screen for phones so that 
      // the tables show correctly 

      if ((url.endsWith("stepfirst/") || url.endsWith("stepsecond/"))&& !isTablet) { 
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
      } else if (!isTablet && !url.endsWith("edit/")){ 
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
      } 

     } 

     @Override 
     public WebResourceResponse shouldInterceptRequest(WebView view, 
       String url) { 
      // TODO Auto-generated method stub 
      Log.d(TAG, "Inside #shouldInterceptRequest url :" + url); 

      //This is a hack as the "edit" url isn't firing the onPageFinished. 
      if (url.contains("projects") && url.contains("edit")) { 
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
      }else if(url.contains("users") || url.endsWith("help/") || url.endsWith("projects/")){ 
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 


      } 
      return super.shouldInterceptRequest(view, url); 
     } 
    }); 

是的,它是一個黑客,是的,它限制....而Android強迫我的手。

希望它有幫助。

相關問題