Android:如何使用当前应用读取NFC标签

Android:如何使用当前应用读取NFC标签,android,nfc,Android,Nfc,我正在写一个需要读取NFC标签的应用程序。我希望我当前打开的应用程序只是读取附近的标签,而不是通过意图过滤器打开一个新应用程序来读取标签 我曾尝试使用enableForegroundDispatch,但没有成功。每当出现NFC标签时,我的设备只需打开标准标签应用程序即可 到目前为止,我掌握的代码是: final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass()); intent.set

我正在写一个需要读取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èU发现的意图


谢谢

您的代码当前注册前台调度系统,仅在包含NDEF文本记录或文本/纯MIME类型记录的NFC标记时触发。因此,如果您的标签不包含此类记录,则NFC发现事件将传递给其他应用程序,或者在您的情况下,由标准标签应用程序处理

因此,您可能希望注册前台调度系统,以符合您的标记:

IntentFilter[] filters = new IntentFilter[1];
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
filters[0].addCategory(Intent.CATEGORY_DEFAULT);
String[][] techList = new String[][]{ new String[] { NfcA.class.getName() } };
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
或者您可以注册以触发任何标记:

adapter.enableForegroundDispatch(activity, pendingIntent, null, null);

您的代码当前注册前台调度系统,以便仅在包含NDEF文本记录或文本/纯MIME类型记录的NFC标记时触发。因此,如果您的标签不包含此类记录,则NFC发现事件将传递给其他应用程序,或者在您的情况下,由标准标签应用程序处理

因此,您可能希望注册前台调度系统,以符合您的标记:

IntentFilter[] filters = new IntentFilter[1];
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
filters[0].addCategory(Intent.CATEGORY_DEFAULT);
String[][] techList = new String[][]{ new String[] { NfcA.class.getName() } };
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
或者您可以注册以触发任何标记:

adapter.enableForegroundDispatch(activity, pendingIntent, null, null);

首先,您必须在AndroidMenifest.xml文件中获得nfc的权限。权限为:

<uses-permission android:name="android.permission.NFC" />

<uses-feature android:name="android.hardware.nfc" />
在onResume回拨中,启用前台调度以检测NFC意图

mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
在onPause回调中,您必须禁用ForGrand分派:

if (mAdapter != null) {
    mAdapter.disableForegroundDispatch(this);
}
在onNewIntent回调方法中,您将获得新的Nfc意图。获取意图后,必须解析意图以检测卡:

    @Override
protected void onNewIntent(Intent intent){    
    getTagInfo(intent)
     }
private void getTagInfo(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
现在你有标签了。然后,您可以检查标签技术列表以检测该标签。标签检测技术在本文中介绍
完整的项目在

首先,您必须在AndroidMenifest.xml文件中获得nfc的权限。权限为:

<uses-permission android:name="android.permission.NFC" />

<uses-feature android:name="android.hardware.nfc" />
在onResume回拨中,启用前台调度以检测NFC意图

mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
在onPause回调中,您必须禁用ForGrand分派:

if (mAdapter != null) {
    mAdapter.disableForegroundDispatch(this);
}
在onNewIntent回调方法中,您将获得新的Nfc意图。获取意图后,必须解析意图以检测卡:

    @Override
protected void onNewIntent(Intent intent){    
    getTagInfo(intent)
     }
private void getTagInfo(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
现在你有标签了。然后,您可以检查标签技术列表以检测该标签。标签检测技术在本文中介绍
完整项目在

标签上有哪些数据?你想在某个标记类型上触发吗?请参阅android.nfc.tech.*名称空间或在任何标记上?我需要的唯一一件事,我认为标记上唯一的一件事就是ID。这并不能真正回答我的问题。好的,对不起。。。标记类型是MifareClassic,所以我可能只想触发它。我只对标签ID感兴趣。您的标签上有哪些数据?你想在某个标记类型上触发吗?请参阅android.nfc.tech.*名称空间或在任何标记上?我需要的唯一一件事,我认为标记上唯一的一件事就是ID。这并不能真正回答我的问题。好的,对不起。。。标记类型是MifareClassic,所以我可能只想触发它。我只对标签ID感兴趣。