Android:使用我的自定义方案重定向到网址不起作用

时间:2014-12-17 09:18:05

标签: redirect android-intent webview

我想SOF对于这样一个主题有一些答案,但是仍然有些东西对我无效。 我在WebView中从某个站点重定向到URL类型为“myapp:// something”的东西。之前,此重定向是由站点的API进行的,其中应用程序已经注册,以使用上述方案获取URL回调。记录重定向,在那里确认URL“myapp:// something”,但是,例如,在WebView中重定向到http://some.host可以导向外部浏览器或(当WebViewClient.shouldOverrideUrlLoading设置为返回false时)到相同的WebView,使其尝试打开URL而不是让系统发送意图。 在上面提到的两种情况下,重定向到myapp://某些东西都无处可去,尽管我为处理它而创建的活动的意图过滤器设置如下:

<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的错误,还是我的意图过滤器设置不正确?

1 个答案:

答案 0 :(得分: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的一个bug或一个功能,我不知道。但在这种情况下,显式的Intent调用是唯一能够稳定且完美运行的东西。