Android 应用只为一个 Activity 启用 NFC

Android app enable NFC only for one Activity(Android 应用只为一个 Activity 启用 NFC)

本文介绍了Android 应用只为一个 Activity 启用 NFC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为启用 NFC 的应用程序在 android 中仅启用一个活动 NFC?

我读过这个,仅从特定活动中读取 NFC 标签

但设备仍在扫描应用程序所有活动的标签.

<?xml version="1.0" encoding="utf-8"?><清单 xmlns:android="http://schemas.android.com/apk/res/android"包="com.example.nfccheckout" ><用途-特征android:name="android.hardware.nfc"android:required="true"/><使用权限 android:name="android.permission.NFC"/><应用机器人:allowBackup="真"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><活动android:name=".activities.MainActivity"android:label="@string/app_name" ><意图过滤器><action android:name="android.intent.action.MAIN"/><类别 android:name="android.intent.category.LAUNCHER"/></意图过滤器></活动><活动android:name=".activities.ReceiveActivity"android:label="@string/title_activity_receive" ><意图过滤器><action android:name="android.nfc.action.NDEF_DISCOVERED"/><类别 android:name="android.intent.category.DEFAULT"/><数据 android:mimeType="application/json+com.example.nfccheckout"/></意图过滤器></活动><活动android:name=".activities.CreatePayloadActivity"android:label="@string/title_activity_create_payload" ></活动><活动android:name=".activities.ConfirmationActivity"android:label="@string/title_activity_confirmation" ></活动></应用程序></清单>

解决方案

如果你想禁用处理 NFC 发现事件 (NDEF_DISCOVERED, TECH_DISCOVERED, TAG_DISCOVERED)当某个活动在前台时,您将为 该活动/advanced-nfc.html#foreground-dispatch" rel="noreferrer">前台调度系统.然后该活动可以忽略这些事件(它将在其 onNewIntent() 方法中接收.

这将阻止将 NFC 发现事件传递给 任何在清单中注册了 NFC 发现意图过滤器的其他活动(即在您的应用和任何其他已安装应用中的活动).p>

但是,此方法不会禁用设备的 NFC 调制解调器.所以 NFC 芯片仍然会轮询标签,但它们不会被报告给任何应用程序.

因此,您想要禁用 NFC 的所有活动都会执行以下操作:

public void onResume() {超级.onResume();NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);}公共无效 onPause() {超级.onPause();NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);nfcAdapter.disableForegroundDispatch(this);}公共无效onNewIntent(意图意图){if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {//丢弃 NFC 事件}}

Is it possible to enable NFC for only one activity in android for an NFC enabled application?

I've read this, Reading NFC tags only from a particuar activity

But the devices are still scanning for tags on all activities of the application.

EDIT:

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".activities.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.ReceiveActivity"
            android:label="@string/title_activity_receive" >
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="application/json+com.example.nfccheckout" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.CreatePayloadActivity"
            android:label="@string/title_activity_create_payload" >
        </activity>
        <activity
            android:name=".activities.ConfirmationActivity"
            android:label="@string/title_activity_confirmation" >
        </activity>
    </application>

</manifest>

解决方案

If you want to diable processing of NFC discovery events (NDEF_DISCOVERED, TECH_DISCOVERED, TAG_DISCOVERED) while a certain activity is in the foreground, you would register that activity for the foreground dispatch system. That activity can then ignore these events (which it would receive in its onNewIntent() method.

This will prevent NFC discovery events to be delivered to any other activities (so those in your app and in any other installed app) that have an NFC disovery intent filter registered in the manifest.

However, this method will not disable the device's NFC modem. So the NFC chip will still poll for tags but they will just not be reported to any app.

So all activities that you want to disable NFC for would do something like this:

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

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

public void onNewIntent(Intent intent) {
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        // drop NFC events
    }
}

这篇关于Android 应用只为一个 Activity 启用 NFC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android 应用只为一个 Activity 启用 NFC

基础教程推荐