Android 应用仅为一个 Activity 启用 NFC

标签 android android-intent tags nfc intentfilter

是否可以为支持 NFC 的应用程序在 android 中仅为一个 Activity 启用 NFC?

我读过这个, Reading NFC tags only from a particuar activity

但设备仍在扫描应用程序所有 Activity 的标签。

编辑:

<?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>

最佳答案

如果你想在某个 Activity 在前台时禁用 NFC 发现事件(NDEF_DISCOVEREDTECH_DISCOVEREDTAG_DISCOVERED)的处理,您将为 foreground dispatch system 注册该 Activity .然后该 Activity 可以忽略这些事件(它将在其 onNewIntent() 方法中接收这些事件。

这将阻止 NFC 发现事件传递给在 list 中注册了 NFC 发现 Intent 过滤器的任何其他 Activity (例如您的应用和任何其他已安装应用中的 Activity )。 p>

但是,此方法不会禁用设备的 NFC 调制解调器。因此 NFC 芯片仍会轮询标签,但不会将它们报告给任何应用。

因此,您想要禁用 NFC 的所有 Activity 都将执行如下操作:

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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24059027/

相关文章:

android - 有什么办法可以强制不重新创建 IntentService 吗?

android - 密码和 ConfirmPassword 验证 Android 不工作

svn - Grails-创建标签并发布版本

mysql - 来自多个表的标签

AndroidManifest错误: android:exported needs to be explicitly specified for <activity>

android - Android Studio 中的 ffmpeg

android - 方法 View.getForeground() 需要 api 23

android - 如何不打开已通过最近的应用程序完成的 Activity

git - 当你修改被标记的提交时,Git 中的标记会发生什么?

android - 如何自定义共享 Intent 中显示的图标和标签?