Don't show App Picker if current activity can handle NFC - android

I'm writing a handful of NFC-capable apps for work.
I've got two of them on my tablet right now, and they are set to launch if an NFC tag is detected and they're not already open. So since I have two of them on the same device now I get an App Picker dialog. This is great.
What I would like is to make it so that if one of the apps is already open, that when the NFC tag is detected it doesn't show the app picker, but just uses the current activity to handle the NFC intent. How possible is this? Thanks

If the foreground activity is using enableForegroundDispatch(), it will take precedence over anything else registered in the manifest for the tag.
Here is a sample app that demonstrates the use of enableForegroundDispatch(), to write text shared from another app (e.g., URL from the Browser) to an NFC tag.

Related

Reading NFC card using a button(Isodep)

I am quite new to android development and trying to read an nfc card and was wondering if it's possible to read it when the user presses a button. I know how to read a card once the card is near the reader or when it is tapped to the nfc reader (onTagDiscovered). I would like to know if it's possible when a user presses a button then the device will read the card.
Android not working as this.
You could declare in your AndroidManifest that an Activity in your application could read NFC tag.
When a tag is detected by system,
if only your application declared that could reading NFC tags then your Activity is launched with discovered tag in Intent
if multiple application declared that could reading NFC tags
then a "popup" is shown to user to choose application
The other solution
You use enableForgroundDispatch : https://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html#foreground-dispatch
The foreground dispatch system allows an activity to intercept an
intent and claim priority over other activities that handle the same
intent. Using this system involves constructing a few data structures
for the Android system to be able to send the appropriate intents to
your application. To enable the foreground dispatch system
If multiple applications "listen" to NFC tag and your Activity is in foreground and enableForgroundDispatch then only your application, receive NFC discovered Tag .
With previous mechanism, you could catch all tags in your Activity and when user press a button do required action, when user not press button, do nothing.

How to prevent other apps to read NFC tags when my app is in foreground?

I want to avoid the dialog that pops up displaying the different apps available to read a NFC Tag when my app is in foreground. Is that even possible?
Checkout http://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html
Specifically, Using the Foreground Dispatch System section

Disable NFC functionality from within the app

The issue I'm trying to solve is as follows:
An NFC tag will trigger my app, from which I'm launching another app. What sometimes will happen is that the NFC tag will move and will be "re discovered" by the phone and launch my app once again. The moving of the NFC tag is something I can't control.
This really ruins the experience.
What I would ideally like to do is once my app is launched, NFC triggering will be disabled or paused, and once app is closed (OnDestroy()) I will enable / un-pause NFC functionality.
I couldn't find how this could be done and your help would be really appreciated. If you have another (code related) approach on how this can be solved, this is just as good.
I noticed this post from a couple years back : NFC disable adapter
But I hope there is another solution, being a system application is not an option and prompting the user the disable NFC is just as bad...
Thanks in advance.
You can create an activity-alias that handles the intent, disable it while your activity is running and re-enable it when it is destroyed.
You're assuming that you can saftely turn off an important functionality of the phone, and disable any other service and/or app that is dependant on it.
This is of course wrong. You should only make assumptions regarding your own app.
So what you're really trying to do is to disable the re-launch. This can be done in many ways:
Control your main Activity's launch mode, and thus your entire app's.
Handle the NFC intent correctly, via a Service that monitors the NFC state as well as your app's. As per #Michael's remark - since you can only associate an NFC intent with an Activity - you would have to create a non-ui Activity (one that does not call setContentView) to act as a BroadcastListener for this Service.
Tag your app, as suggested by #Tristan.

Write an NFC application that preempts all other NFC applications

Is there a way to make an NFC application read a tag over all other applications that may be downloaded on the phone?
Usually, if more than one application can read a given tag, then the user is prompted with which application to open. I would like to skip this step and have my application automatically open.
Any resources or actual code would be really helpful. This is just part of a research project so I don't even need to code the application, I would just like to know if it is possible and potentially build a proof of concept application if I have time.
No, but you can use filters to specify a specific NDEF tag type to look for. In that case only your app would be brought up, but if another app is also looking for that exact same tag type, both apps will be brought up.

Need app to detect nfc, both when running and when closed

I'm currently building an application for Android using NFC-technology. The NFC-part is being used to log in to the app. So here's the deal: i want the app to respond differently when detecting the tag when it is already running. Here are the two situations:
1) When the user scans a tag with a specific URI, the app launches, loading the URI into the username-textfield. User then enters his password, presses login, and voila, magic. This part works fine.
2) Now, i also want the app to be launched from the app-list, showing a login form. No problem, i use a different activity for that. But now, how do i make some sort of custom Event Listener that makes the app wait for the tag to be scanned, and then puts the URI into the username-field without, and here it comes, launching the app again, as described in situation 1?
Hope you guys can help me out here, sorry for the long text.
Use foreground mode for your open activity and it will be able to receieve all NDEF messages (from tag or beam). Check boilerplate project in this project (shameless plug).
Your application itself simply needs to be aware of it's own current state and respond to the scanned tag accordingly

Categories

Resources