6

Is it possible to enable NFC for only one activity in android for an NFC enabled application?

I've read this, Reading NFC tags only from a particuar activity

But the devices are still scanning for tags on all activities of the application.

EDIT:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nfccheckout" >

    <uses-feature
        android:name="android.hardware.nfc"
        android:required="true" />

    <uses-permission android:name="android.permission.NFC" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".activities.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.ReceiveActivity"
            android:label="@string/title_activity_receive" >
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="application/json+com.example.nfccheckout" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.CreatePayloadActivity"
            android:label="@string/title_activity_create_payload" >
        </activity>
        <activity
            android:name=".activities.ConfirmationActivity"
            android:label="@string/title_activity_confirmation" >
        </activity>
    </application>

</manifest>
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
Tiago Veloso
  • 8,513
  • 17
  • 64
  • 85
  • Please post your manifest. – CommonsWare Jun 05 '14 at 11:40
  • @CommonsWare added the manifest – Tiago Veloso Jun 05 '14 at 12:59
  • If you want to scan Nfc when that particular activity is running, then use the @Michael Roland's solution and also remove the intent filter from manifest for that activity. – Ravi Bhatt Jun 06 '14 at 06:17
  • 1
    Please, set @Michael 's solution as your problem solution. It really works. – Kaitiff Apr 29 '15 at 13:51
  • Try this, which is essentially setting an empty intent as your PendingIntent in the activities in which you don't want anything to happen with NFC (and also don't want Android's default reader to pop up): https://stackoverflow.com/a/57237196/5147164 – policenauts Jul 28 '19 at 01:07

2 Answers2

10

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
    }
}
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
1

Your ReceiveActivity is set up to be triggered any time an NDEF tag with the specified MIME type is encountered. If you do not want this behavior, you need to do something, such as removing this <intent-filter>.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491