【发布时间】:2014-09-01 16:59:27
【问题描述】:
我正在编写一个需要读取 NFC 标签的应用程序。而不是通过意图过滤器读取标签打开一个新应用程序的行为,我希望我当前打开的应用程序只是读取附近的标签。
我曾尝试使用 enableForegroundDispatch() 但没有成功。每当出现 NFC 标签时,我的设备只会打开标准的“标签”应用程序。
我目前的代码是:
final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
IntentFilter[] filters = new IntentFilter[1];
String[][] techList = new String[][]{};
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);
(基本上是从我找到的教程中复制的)。
有人知道这是否是正确的方法吗? 如何防止打开标准的“标签”应用程序而不是我自己的应用程序接收 NDEF_DISCOVERED 意图。
谢谢
【问题讨论】:
-
您的标签上有哪些数据?您想触发特定的标签类型(参见
android.nfc.tech.*命名空间)还是任何标签? -
我唯一需要的(我认为标签上唯一的东西)就是 ID
-
这并不能真正回答我的问题。
-
好的,抱歉...标签类型是 MifareClassic,所以我可能只想触发它。我只对标签 ID 感兴趣。