How To Avoid Launching An Nfc-enabled App?
Assuming that I have 2 activities: MainActivity and SecondActivity. What i want to achieve is to pass from MainActivity to SecondActivity by discovering an NFC tag. I made it wor
Solution 1:
You could register for the foreground dispatch in your MainActivity
. Then, upon receiving the NFC intent, you can start the SecondActivity
and pass the intent to it:
@OverridepublicvoidonResume() {
super.onResume();
NfcAdapteradapter= NfcAdapter.getDefaultAdapter(this);
PendingIntentpendingIntent= PendingIntent.getActivity(
this, 0, newIntent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
adapter.enableForegroundDispatch(this, pendingIntent, null, null);
}
@OverridepublicvoidonPause() {
super.onPause();
NfcAdapteradapter= NfcAdapter.getDefaultAdapter(this);
adapter.disableForegroundDispatch(this);
}
@OverridepublicvoidonNewIntent(Intent intent) {
Stringaction= intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) ||
NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) ||
NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
IntentnewIntent=newIntent(this, SecondActivity.class);
newIntent.putExtra("NFC_INTENT", intent);
startActivity(newIntent);
}
}
Solution 2:
If I correctly understood your question, the problem is that your activity triggers also when the app is not running.
If this is the point, the problem is that you've declared your activity to be triggered on NFC event in the AndroidManifest.xml file and the solution is to remove the NFC block from the activity declaration in the manifest.
Post a Comment for "How To Avoid Launching An Nfc-enabled App?"