3

My previous question established that when a factory reset Android device taps an NFC tag which contain an URL, the operating system launches a browser.

Can I prevent that?

Is that just a default action on the part of the o/s, since no other handler was available?

If I code a BroadcatReceiver to handle the tap, will that automatically prevent the o/s from launching the browser? If not, can I add code to suppress the browser launch?

2

The browser opens because no other app can handle this action. If you will have 2 browsers the OS will ask you which app do you want to handle this action and when you choose one the OS will remember your preference.

You always can go to settings (the app) and "clear default". So, to your question you can write an app that will handle this action (by write this Broadcast receiver in the  AndroidManifest.xml) and don't do anything with the URL. And then tapping on the NFC tag and choose your app and click always (so it'll not ask again). This way you can force the OS to not do anything when you tapping on an NFC tag with a URL.

  • Thanks for the info! Actually, I do want my Broadcast Receiver to launch a browser, but I want it to add a few extra POST or GET parameters for certain URLs. Sounds doable, don't you agree? – Mawg Jan 23 '15 at 8:55
  • 1
    @Mawg Yeah, of course. You can do that with a URI (developer.android.com/reference/java/net/URI.html). – Nir Jan 24 '15 at 9:21
1

From a user's perspective, you cannot really prevent that a URL retrieved from an NFC tag is launched in a browser. Upon discovery of such a URL, Android just notifies the application that previously registered its interest into that data type coming from an NFC tag (or shows an activity chooser if multiple applications registered an interest into that data type).

Hence, it's the web browser application that registered its interest (through an intent filter in its AndroidManifest.xml) and you cannot normally1 disable this.

From a programmer's perspective, however, you can prevent that a particular URL is handled by a web browser. Web browsers usually register to be launched upon all "http://" and "https://" URLs. You can override this by registering your application for a more specific URL (i.e. one that includes a host name or even a path, e.g. http://www.example.com/). In that case your application will automatically get precedence over the web browser (no activity chooser will be shown).

Regarding your idea with the BroadcastReceiver: This is currently not possible. NFC discovery events, like the discovery of a URL, are only sent to activities. A broadcast receiver cannot pick up these intents. (See this question on Stackoverflow for a possible workaround.)



1) Note that this is not entirely true as there are some means to disable intent filters programatically under certain conditions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.