我想 SOF 上有一些针对此类主题的答案,但仍然有些东西对我不起作用。
重要的是我在 WebView 中从某个站点重定向到类似“myapp://something”的 URL。之前,此重定向是由站点的 API 进行的,其中应用程序已注册以使用上述方案获取 URL 回调。重定向已记录,URL“myapp://something”已在那里确认,但是,例如,重定向到http://some.host http://some.host内部 WebView 可以被引导到外部浏览器或(当 WebViewClient.shouldOverrideUrlLoading 设置为返回 false 时)到同一个 WebView,使其尝试打开 URL,而不是让系统发送意图。
在上面提到的两种情况下,重定向到 myapp://something 不会导致任何结果,尽管我为处理它而创建的活动的意图过滤器的设置如下:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp" />
</intent-filter>
这是提出的一种解决方案Android url 覆盖不适用于重定向 https://stackoverflow.com/questions/15501990/android-url-override-doesnt-work-on-redirect
and in http://developer.appcelerator.com/question/120393/custom-url-scheme---iphone--android http://developer.appcelerator.com/question/120393/custom-url-scheme---iphone--android有人可以告诉我,这是 WebView 的错误,没有有效的重定向,还是我的意图过滤器设置不正确?
好吧,我终于得到答案了。无论如何,从 WebView 重定向到自定义方案 URL 是不可能的。这就是为什么应该进行显式 Intent 调用,并使用此自定义方案 URL 字符串作为附加数据。
在代码中,这看起来像这样:
WebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//called for any redirect to stay inside the WebView
if (url.contains("myapp")) { //checking the URL for scheme required
//and sending it within an explicit Intent
Intent myapp_intent = new Intent(Intent.ACTION_VIEW);
myapp_intent.setData(Uri.parse(url));
myapp_intent.putExtra("fullurl", url);
startActivity(myapp_intent);
return true; //this might be unnecessary because another Activity
//start had already been called
}
view.loadUrl(url); //handling non-customschemed redirects inside the WebView
return false; // then it is not handled by default action
}
所以也许这是 WebView 的一个错误或一个功能,我不知道。但在这种情况下,显式的 Intent 调用是唯一稳定且完美运行的方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)