我正在编写需要读取NFC标签的应用。而不是阅读标签通过意图过滤器打开一个新的应用程序的行为,我希望我目前打开的应用程序只需读取附近的标签。Android:如何使用当前应用读取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。*'命名空间)或任何标签? – 2014-09-01 17:46:40
我需要的唯一东西(我认为标签上唯一的东西)是ID – chris 2014-09-02 08:05:18
这并不能真正回答我的问题。 – 2014-09-02 08:40:35