问题
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>
回答1:
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
}
}
回答2:
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>
.
来源:https://stackoverflow.com/questions/24059027/android-app-enable-nfc-only-for-one-activity