简体   繁体   English

Android Intent Filters:如何正确使用Intent.CATEGORY_BROWSABLE?

[英]Android Intent Filters : How do you use Intent.CATEGORY_BROWSABLE correctly?

I have an intent filter to intercept urls from the browser on my domain. 我有一个意图过滤器,用于拦截来自我域中浏览器的URL。 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>

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. 我创建了一个带有onClick的按钮,该按钮仅创建View的意图以及表示该意图的URL。 This will present the user with the choice dialog again. 这将再次向用户显示选择对话框。

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. 因此,如果我正在阅读的博客文章中提到了特定的URL和该文档的特定部分,则截取该URL不是用户想要的,而是会将其发送给本机应用程序。 Also then upon selecting the button with the intent, they will immediately be sucked back into the application. 同样,在有意选择按钮后,它们也将立即被吸回到应用程序中。

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). 如何为系统浏览器构造ACTION_VIEW意图,或者至少检测到用户选择了始终使用本机应用程序,以便可以使用ACTION_WEB_SEARCH(它与历史记录堆栈具有不同的UI行为,所以我不会这样做)不要选择一直使用此操作)。

So I have retouched this again and the answer is actually pretty simple. 因此,我再次进行了润饰,答案实际上非常简单。 Use Intent.createChooser(intent, charSequence). 使用Intent.createChooser(intent,charSequence)。 Whatever is chosen seems to bypass the saved preferences of the OS. 无论选择什么,似乎都会绕过OS的已保存首选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM