I got to solve the following problem (if its even possible): I'm working on an Android app, which loads information from the internet, corresponding to the scanned NFC tag.
As long as the activity is in foreground, I receive the intent with the action ACTION_TAG_DISCOVERED
(through the foreground dispatch system), get the NFC ID of the intent and proceed.
But it was requested, to show the information form the scanned tag directly, even if the activity is not yet in the foreground.
Starting/Brining the app back to foreground works by writing the package name in an Android Application Record (AAR) to the NFC tag, but I'm not receiving the intent with the ID of the tag, without scanning the tag a second time, after the activity was opened.
Is there any possibility to start an activity and get the tag ID of the NFC tag by scanning an NFC tag only once? All sources I researched now said, that an activity running in foreground is required to obtain the intent, containing the ID of the scanned tag.
EDIT:
Trying to explain my problem more exactly. What I got yet:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="vnd.android.nfc" />
</intent-filter>
My Activity:
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
if (intent.getByteArrayExtra(NfcAdapter.EXTRA_ID) != null) {
byte[] TagId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)
…
}
}
}
This works fine, as long as the activity is running. Now, the activity is in background or was not created yet. I scan an NFC tag, and the activity starts, because the package was written to this NFC tag. After starting, the ID of the scanned tag should be available, without scanning the tag an second time
I'm not sure I understand what problem you are having but I have used NFC tags in the past this way.
Put this intent-filter on your Activity in the manifest:
note: you'll have to change "myapp" to whatever scheme your NFC tags use
Then inside your activity you should be able to do something like this to get the data from your tag:
This will launch your activity even if it wasn't running before.