【发布时间】:2014-06-22 01:27:46
【问题描述】:
我正在尝试使用前台调度读取 Mifare Classic 1k 卡的 ID。从我的日志中可以看出,我可以启用前台调度,但不能调用 onNewIntent() 方法。任何建议将不胜感激。
MainActivity.java
...
@Override
protected void onResume() {
setupForegroundDispatch(this, mAdapter);
super.onResume();
}
public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
System.out.println("Setup FGD."); // i can see this output.
final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
IntentFilter[] filters = new IntentFilter[1];
String[][] techList = new String[][]{};
// Notice that this is the same filter as in our manifest.
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
filters[0].addCategory(Intent.CATEGORY_DEFAULT);
try {
filters[0].addDataType(MIME_TEXT_PLAIN);
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("Check your mime type.");
}
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
System.out.println("Enabled FGD."); // and this one.
}
protected void onNewIntent(Intent intent) {
System.out.println("Intent."); but i cannot see this one,
handleIntent(intent);
}
private void handleIntent(Intent intent) {
System.out.println("Handle."); // and this one.
String action = intent.getAction();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
System.out.println("NDEF discovered.");
....
AndroidManifest
....
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<action android:name="android.nfc.action.TAG_DISCOVERED" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>
....
【问题讨论】:
-
您的标签是否包含一条以 Text 记录(或 MIME 类型为 text/plain 的 MIME 类型记录)作为第一条记录的 NDEF 消息?
-
实际上,当我将 ACTION_NDECF_DISCOVERED 之后的 ACTION_TECH_DISCOVERED 操作添加到我的 NfcAdapter 时,它就像魔术一样解决了。但也感谢您的解决方案。
-
它并没有真正“像魔术一样解决”,但实际上回答了我的问题:所以你的标签不包含这样的 NDEF 记录,你需要一种更通用的方法来检测标签(通过它的标签技术而不是它的内容)。
标签: android android-intent nfc intentfilter mifare