[英]How to read/write NFC card in different activities
I have two activities. 我有两个活动。 I want to use the first activity (as
MainAcitivity
) to read cards, and the second activity to write cards. 我想使用第一个活动(如
MainAcitivity
)来读取卡,然后使用第二个活动来写入卡。 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. 但是,我的问题是,当我参加第二个活动并扫描NFC卡时,电话将显示第一个活动和第二个活动的意图选择器。
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? 因此,当我处于第二个活动(以及相反)中时,如何通过代码禁用第一个活动的
NDEF_DISCOVERED
意图过滤器?
This is the full AndroidManifest file: 这是完整的AndroidManifest文件:
<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>
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 . 为了强制当前前台中的活动接收NFC发现事件,您可以使用前台调度系统或阅读器模式API 。
Both of these methods will give an activity precedence in receiving NFC discovery events if it is currently in the foreground. 如果当前处于前台,则这两种方法都将在接收NFC发现事件时给予活动优先级。
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. 此外,如果您不希望通过NFC发现事件启动活动,则无需在该活动的mainfest中声明
android.nfc.action.*_DISCOVERED
意向过滤器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.