Android Intent Filters : How do you use Intent.CAT

2019-04-15 14:43发布

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>

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.

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.

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

1条回答
Rolldiameter
2楼-- · 2019-04-15 15:19

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.

查看更多
登录 后发表回答