Android的意图过滤器:你如何正确使用Intent.CATEGORY_BROWSABLE?过滤器、意图、如何正确、Android

2023-09-07 13:10:30 作者:院长放我出院

我有一个意图过滤器,以从我的域名,浏览器拦截的URL。这会给用户使用本机应用程序或浏览器的选择。我已经安装意图过滤如此如下​​。

I have an intent filter to intercept urls from the browser on my domain. This will give the user the the choice of using the native application or the browser. I have setup the following intent filter as so.

<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:host="myDomain" android:pathPrefix="/custom/" android:scheme="http" />
 </intent-filter>

现在,这不是我的本意捕获用户在本机应用程序,因为从使用浏览器多少好处。所以,我想给用户一些能够回到浏览器的应用程序中。我已创建了一个onClick一个按钮,简单地创建视图的意图,并且将重新present的URL。这将present与再次选择对话框中的用户。

Now, it's not my intention to trap the user in the native application as there is much benefit from using the browser. So I would like to give the user some ability to go back to the browser within the application. I have created a button with an onClick that simply creates an intent for View and the url that would represent it. This will present the user with the choice dialog again.

锁扣,这是一个,使用者也可以选择一个复选框,始终使用本地应用程序。虽然这是很好,有时候绝对是错误的。所以,如果我读一篇博客文章中说提及特定的URL和文件的特定部分,拦截的URL是不是用户想要,但将其发送给本机应用程序。还然后在与意图选择按钮,它们将立即被吸回应用程序

The catch to this is that a user is also allowed to select a checkbox to always use the native application. While that is well and good, sometimes absolutes are wrong. So if I am reading a blog post that mentions a specific url and a specific part of that document, intercepting the url is not what the user intended but will send them to the native application. Also then upon selecting the button with the intent, they will immediately be sucked back into the application.

你怎么能构建一个ACTION_VIEW意图为系统浏览器,或者至少检测到用户选择始终使用本机应用程序,让您可以使用ACTION_WEB_SEARCH(它与历史堆栈不同的UI行为,这是为什么我不会选择使用这个动作所有的时间)。

How can you structure a ACTION_VIEW intent for the system browser or at least detect that the user has selected always use the native app so that you can use the ACTION_WEB_SEARCH (it has a different UI behavior with the history stack, which is why I wouldn't opt to use this action all the time).

推荐答案

所以我再修整这一点,答案其实是pretty简单。使用Intent.createChooser(意向,为CharSequence)。无论选择似乎绕过OS的保存preferences

So I have retouched this again and the answer is actually pretty simple. Use Intent.createChooser(intent, charSequence). Whatever is chosen seems to bypass the saved preferences of the OS.