Complient链接URL不会在WebView KitKat 4.4.4中调用onPageFinished(Complient link URL does not call onPageFinish

编程入门 行业动态 更新时间:2024-10-24 01:55:52
Complient链接URL不会在WebView KitKat 4.4.4中调用onPageFinished(Complient link URL does not call onPageFinished in WebView KitKat 4.4.4)

我有一个适用于所有Api> 11的应用程序,包括Kitkat 4.4.2。

但是,在KitKat 4.4.4中,onPageFinished()没有针对1个链接触发:

HREF = “http://app.host.com/projects/30/edit/”

我知道KitKat WebView问题,据我所知,这个URL应该有效。

与此URL对应的Web应用程序以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); } });

I have an app that works correctly on all Api's>11 including Kitkat 4.4.2.

However, in KitKat 4.4.4 the onPageFinished() is not firing for 1 link :

href="http://app.host.com/projects/30/edit/"

.

I know about the KitKat WebView issue, and from what I know this URL should work.

The web app that corresponds to this URL is written in RoR and has turbolinks disabled.

My WebView client is shown below.

I have spent days in trying to get this to work.

If anybody sees anything that is wrong, please let me know.

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

最满意答案

所以,这是一个我决定使用的丑陋黑客:

在onPageFinished()时,onPageStart()和shouldOverrideUrlLoading()并不总是被URL调用,即使它们格式正确。

我发现shouldInterceptRequest()是由我尝试过的所有无法比较的url触发的(只有限且不完全科学)并且它为我提供了相同的数据来控制url的流量,尽管在页面加载的早期阶段。

所以,我使用了shouldInterceptRequest()而不是VERY限制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强迫我的手。

希望能帮助到你 。

So, this is an ugly hack that I decided to use:

While onPageFinished(), onPageStart() and shouldOverrideUrlLoading() is not always called by URL's, even if they are correctly formatted.

I found that shouldInterceptRequest() is fired by all incomparable url's I have tried (only limited and not fully scientific) and it gives me the same data for controlling the flow by url, although in an earlier stage in the page load.

So, I used shouldInterceptRequest() instead of the VERY restricted onPageFinished(), onPageStart() and shouldOverrideUrlLoading().

here is the code :

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

Yes it's a hack, yes it's limited.... but Android forced my hand.

Hope it helps .

更多推荐

本文发布于:2023-08-04 04:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:会在   链接   WebView   Complient   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!