0

I have two activities. I want to use the first activity (as MainAcitivity) to read cards, and the second activity to write cards. Because the activity needs to be active when the card is discovered. Thus, I used the below setting for both activities:

        </intent-filter>
        <!-- Handle notes detected from outside our application -->
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>

However, my problem is that when I am in the second activity and I scan an NFC card, the phone will show an intent chooser for both, the first and the second activity.

So, how can I disable the NDEF_DISCOVERED intent filter of the first activity while I am in the second activity (and the other way round) by code?

This is the full AndroidManifest file:

   <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".FirstActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|screenSize|screenLayout"
             >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!-- Handle notes detected from outside our application -->
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>
        <activity 
            android:name=".SecondActivity"
            android:label="@string/app_name">
           <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>
    </application>

1 Answer 1

0

In order to force NFC discovery events to be received by the activity that is currently in the foreground, you could use either the foreground dispatch system or the reader-mode API.

Both of these methods will give an activity precedence in receiving NFC discovery events if it is currently in the foreground.

So you could, for instance, use something like:

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);

    // to catch all NFC discovery events:
    nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);

    // or to only catch text/plain NDEF records (as you currently do in your manifest):
    //IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    //try {
    //    ndef.addDataType("text/plain");
    //} catch (MalformedMimeTypeException e) {}
    //nfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[] { ndef }, null);
}

public void onPause() {
    super.onPause();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.disableForegroundDispatch(this);
}

public void onNewIntent(Intent intent) {
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) ||
        NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()) ||
        NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        // handle NFC events ...
    }
}

Moreover, if you do not want an activity to be started by NFC discovery events, you do not need to declare an android.nfc.action.*_DISCOVERED intent filter in the mainfest for that activity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.