【问题标题】:Android: Redirect to a URL with my custom scheme doesn't workAndroid:使用我的自定义方案重定向到 URL 不起作用
【发布时间】:2014-12-17 09:18:04
【问题描述】:
我想对于这样的主题有一些关于 SOF 的答案,但仍然有些东西对我不起作用。
重要的是我在 WebView 中从某个站点重定向到 URL 类型的“myapp://something”。在之前,此重定向是由站点的 API 进行的,其中应用程序已经注册以使用上述方案获取 URL 回调。重定向已记录,URL "myapp://something" 在那里得到确认,但是,例如,重定向到 WebView 内的http://some.host 可以被引导到外部浏览器或(当 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 override doesn't work on redirect中提出的一种解决方案
在http://developer.appcelerator.com/question/120393/custom-url-scheme---iphone--android
有人可以告诉我,这是 WebView 的错误,没有有效的重定向,还是我的意图过滤器设置不正确?
【问题讨论】:
标签:
redirect
android-intent
webview
【解决方案1】:
好的,我终于得到了答案。不知何故,不可能从 WebView 重定向到自定义方案 URL。这就是为什么应该使用这个自定义的 URL 字符串作为附加数据进行显式 Intent 调用的原因。
在代码中,如下所示:
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 调用是唯一可以稳定且完美运行的方法。