Android app enable NFC only for one Activity
If you want to diable processing of NFC discovery events (NDEF_DISCOVERED
, TECH_DISCOVERED
, TAG_DISCOVERED
) while a certain activity is in the foreground, you would register that activity for the foreground dispatch system. That activity can then ignore these events (which it would receive in its onNewIntent()
method.
This will prevent NFC discovery events to be delivered to any other activities (so those in your app and in any other installed app) that have an NFC disovery intent filter registered in the manifest.
However, this method will not disable the device's NFC modem. So the NFC chip will still poll for tags but they will just not be reported to any app.
So all activities that you want to disable NFC for would do something like this:
public void onResume() {
super.onResume();
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}
public void onPause() {
super.onPause();
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
nfcAdapter.disableForegroundDispatch(this);
}
public void onNewIntent(Intent intent) {
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
// drop NFC events
}
}
Recieve an NFC intents only when activity already started
You can do it by using the foreground dispatch system : http://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html#foreground-dispatch
Also, remove your intent filter from your manifest, it will stop starting your app
Android NFC: one activity to send and one to receive only
You can disable the Beam UI for a specific activity (i.e. your ActivityNFCReceiver
) by using the NfcAdapter.setNdefPushMessage
method:
public void onCreate(Bundle savedInstanceState) {
[...]
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter != null) {
mNfcAdapter.setNdefPushMessage(null, this);
}
}
Be aware that the enableForegroundNdefPush
method has been deprecated since Android 4.0 (API level 14) and that pretty much all Android devices that support NFC use Android 4.0 or later.
While disabling the Beam UI that way works on Nexus devices and is the officially documented way to go (see setNdefPushMessage):
If setNdefPushMessage(...) is called with a null NDEF message [...] then NDEF push will be completely disabled for the specified activity(s). This also disables any default NDEF message the Android OS would have otherwise sent on your behalf for those activity(s).
it seems that -- unfortunately -- this does not work for some Android devices' NFC implementation. For instance, it does not work on Samsung Galaxy S3 running Android 4.3.
Reading NFC tags only from a particuar activity
As a foreground application, your app will always have the chance to react first on the touched tag. So what you need to do is to enable forgraoundDsipatch for your activities, and on Activity1 and Activity3 just to ignore the event, and on Activity2 process the information of the touched tag. Otherwise if your app do not take the event of the detected tag, the android dispatch system will continue to search suitable application to trigger and when it find it it will start it on top of your app.
For more information on Foreground Dispatch look here: http://developer.android.com/guide/topics/nfc/advanced-nfc.html#foreground-dispatch
Start the NFC tag only from my specific application
The only thing I know how to solve this issue is to add an additional AAR record (special NDEF record) on your tag. Subsequently, only your app is starting and nobody is asking anymore.
NFC intent launch app with launcher activity only if the app isn't running in background
I solved my problem by adding following code into launcher activity's onCreate()
. I kept intent filter and meta data same for launcher activity. The trick here is to check if application task is already running into system or not.
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
finish();
return;
}
Above code will check if application task is already running or not. If its running then launcher activity will close him self and will brought top most activity of task to the front.
Reference:
https://stackoverflow.com/a/21022876/2465752
https://developer.android.com/reference/android/R.styleable.html#AndroidManifestActivity_launchMode
Related Topics
Animate Progressbar Update in Android
Android Room - Get the Id of New Inserted Row with Auto-Generate
Android Event for Internet Connectivity State Change
Send Touch Events to a Device via Adb
Extending Relativelayout, and Overriding Dispatchdraw() to Create a Zoomable Viewgroup
Determine When a Viewpager Changes Pages
Remove Vertical Padding from Horizontal Progressbar
Getting "Debuggable" Value of Androidmanifest from Code
How to Import Eclipse Project to Android Studio
Rotate View Hierarchy 90 Degrees
How to Restart Adb Manually from Android Studio
Why Does It Take So Long for Android's Mediaplayer to Prepare Some Live Streams for Playback
How to Install Android Market App on the Emulator
Why Is My Android Emulator Keyboard in Chinese Character Mode
Unable to Execute Adb in Ubuntu. Downloaded File Is Meant for X86-64 While I Have I686