Android:使用我的自定义方案重定向到URL不起作用 [英] Android: Redirect to a URL with my custom scheme doesn't work

查看:193
本文介绍了Android:使用我的自定义方案重定向到URL不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在SOF上有关于此主题的一些答案,但是仍然有些问题对我不起作用. 令人不安的是,我在WebView中从某个站点重定向到URL类型为"myapp://something".在以前,此重定向是由网站的API进行的,该网站的API已经通过上述方案注册了应用程序以获取URL回调.重定向被记录,URL"myapp://something"在那里被确认,但是,例如,重定向到 http://some.host可以在内部将引导至外部浏览器,或者将WebViewClient.shouldOverrideUrlLoading设置为返回false时,将其引导至同一WebView,从而使其尝试打开URL而不是使系统发送意图. 在上述两种情况下,重定向到myapp://something都无济于事,尽管我为处理该活动而创建的活动的意图过滤器设置如下:

I guess there are some answers on SOF for such a topic, but still something doesn't work with me. The thing that mattters is that I get a redirect within WebView from some site to a URL kind of "myapp://something". In before, this redirect is made by site's API where the app is already registered to get a URL callback with the scheme mentioned above. Redirect is logged, URL "myapp://something" is confirmed there, but, for example, redirect to http://some.host inside WebView can be led to external browser or (when WebViewClient.shouldOverrideUrlLoading is set to return false) to the same WebView, making it try to open the URL instead of making system send an intent. In both cases mentioned above, a redirect to myapp://something leads to nowhere, although my intent filter for the activity I created for handling it is set up like that:

<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>

这是建议的解决方案重定向 并在 http://developer.appcelerator.com/question/120393/custom-url-scheme --- iphone--android 有人可以告诉我,这是WebView的错误,没有有效的重定向,还是我的意图过滤器没有正确设置?

That's a kind of solution proposed in Android url override doesn't work on redirect and in http://developer.appcelerator.com/question/120393/custom-url-scheme---iphone--android Could someone tell me, is it a bug of WebView with no working redirects or my intent filter is not set up properly?

推荐答案

好的,我终于得到了答案.从某种程度上说,不可能从WebView重定向到自定义方案URL.这就是为什么应该使用此自定义模式的URL字符串作为附加数据进行显式的Intent调用的原因. 在代码中,如下所示:

Okay, I finally got the answer. Someway that's impossible to redirect from WebView to a custom scheme URL. That's why an explicit Intent call should be made, with this custom-schemed URL string as additional data. In code, this looks like that:

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调用才能稳定且完美地工作.

So maybe this is a bug or a feature of WebView, I don't know. But in this case an explicit Intent call is the only thing that works stable and perfectly well.

这篇关于Android:使用我的自定义方案重定向到URL不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆