android - 如何使用 NFC Action

标签 android broadcastreceiver nfc intentfilter android-broadcast

我正在尝试以编程方式注册接收器,以便在检测到 NFC 标签后收到通知。如我的代码所示,我注册了所需的操作,并以编程方式创建了广播接收器。我还在 list 文件中添加了所需的权限,但问题是永远不会调用 onReceive

请让我知道我做错了什么以及如何解决。

IntentFilter intentFilter1 = new IntentFilter();
intentFilter1.addAction("android.nfc.action.TAG_DISCOVERED");
registerReceiver(mBCR_TAG_DISCOVERED, intentFilter1);

private BroadcastReceiver mBCR_TAG_DISCOVERED = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        mTv.setText("mBCR_TAG_DISCOVERED");
    }
};

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.com.myapplication">

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

<uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
</application>

</manifest>

最佳答案

Intent android.nfc.action.TAG_DISCOVERED 与所有 NFC Intent 一样,是一个 Activity Intent 而不是广播 Intent。根本不可能为它注册一个广播接收器。您可以做的是注册一个 Activity 来接收 NFC Intent 。这可以通过 list 、NFC 前台调度系统或在 Android 4.4+ 上通过 NFC 阅读器模式 API 完成。

1。 list

根据标签上的数据,您可能想要注册 NDEF_DISCOVERED Intent (如果标签上有 NDEF 结构化数据)或 TECH_DISCOVERED Intent (如果您只想监听某些标签技术,而不管标签上的数据)。您通常不想注册 TAG_DISCOVERED Intent 过滤器,因为当通过 AndroidManifest.xml 使用时,它仅用作回退机制(以捕获任何其他应用程序未处理的事件)。

例如如果您的标签包含 URL http://www.example.com/ ,您可以使用以下 Intent 过滤器:

<activity android:name=".MainActivity">
    ...
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="http" android:host="www.example.com" />
    </intent-filter>
</activity>

如果您的标签不包含任何特定数据并且可能采用任何标签技术,您可以使用以下 Intent 过滤器:

<activity android:name=".MainActivity">
    ...
    <intent-filter>
        <action android:name="android.nfc.action.TECH_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
               android:resource="@xml/nfc_tech_filter" />
</activity>

要使此 Intent 过滤器起作用,您还需要在应用程序的 res/ 目录中有一个 XML 资源 xml/nfc_tech_filter.xml。如果 tech 过滤器应该只匹配任何标签,则该文件将包含以下内容:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.NfcA</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcB</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcF</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcV</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcBarcode</tech>
    </tech-list>
</resources>

一旦您的 Activity 注册为接收这些事件,您就可以通过 onCreate()(如果您的 Activity 由 NFC 事件启动)或通过 onNewIntent 在您的 Activity 中接收这些 Intent ()(如果您的 Activity 在打开时收到后续 NFC Intent ):

@Override
public void onCreate(Bundle savedInstanceState) {

    [...]

    Intent startIntent = getIntent();
    if ((startIntent != null) &&
        (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(startIntent.getAction()) ||
        NfcAdapter.ACTION_TECH_DISCOVERED.equals(startIntent.getAction()))) {
        // TODO: process intent
    }
}

@Override
protected void onNewIntent(Intent intent) {
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) ||
        NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
        // TODO: process intent
    }
}

2。前台调度系统

如果您只对接收 NFC 发现 Intent 感兴趣,而您的 Activity 在前台可见,则最好使用 NFC 前台调度系统,而不是通过 list 注册以接收 NFC 事件。您可以通过在 onResume() 期间注册您的 Activity 来执行此操作:

@Override
public void onResume() {
    super.onResume();

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}

您还必须确保在 onPause() 期间取消注册:

@Override
public void onPause() {
    super.onPause();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.disableForegroundDispatch(this);
}

然后您将通过 onNewIntent() 接收 NFC 事件作为 TAG_DISCOVERED Intent :

@Override
public void onNewIntent(Intent intent) {
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        // TODO: process intent
    }
}

3。读者模式API

如果您只对检测 NFC 标签感兴趣,并且仅当您的 Activity 在前台可见并且您只需要以 Android 4.4+ 为目标时,最好的方法可能是使用 NFC 阅读器模式 API。您可以通过在 onStart() 期间注册您的 Activity 来执行此操作:

@Override
public void onStart() {
    super.onStart();

    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.enableReaderMode(this, new NfcAdapter.ReaderCallback() {
        @Override
        public void onTagDiscovered(Tag tag) {
            // TODO: use NFC tag
        }
    }, NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_NFC_B | NfcAdapter.FLAG_READER_NFC_F | NfcAdapter.FLAG_READER_NFC_V | NfcAdapter.FLAG_READER_NFC_BARCODE, null);
}

您还应确保在 onStop() 期间取消注册:

@Override
public void onStop() {
    super.onStop();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.disableReaderMode(this);
}

您通过 onTagDiscovered(Tag tag) 回调方法接收发现的标签句柄。

关于android - 如何使用 NFC Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41632775/

相关文章:

android - 仅在日期选择器 android 中更改所选日期的颜色

android - 如何检测 MEDIA_BUTTON 双击?

android - 多个 Intent ,但广播接收器仅调用一次

android - 标记错误地枚举为 MIFARE Classic,SAK = 32

java - Android使用TargetApi实现接口(interface)

android - 在 USB-NFC-Reader 上访问卡模拟模式

java - 改变 TimePicker (RadialTimePickerView) 的颜色?

android - Android 可以接收 IOS 蓝牙信号吗

android - 应用程序在加载 soundPool 文件 482 次后崩溃

Android BroadCastReceiver 滞后应用程序