Android 应用仅为一项活动启用 NFC [英] Android app enable NFC only for one Activity

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

问题描述

对于支持 NFC 的应用程序,是否可以只为 Android 中的一个活动启用 NFC?

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

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

<uses-permission 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"/><category android:name="android.intent.category.LAUNCHER"/></意图过滤器></活动><活动android:name=".activities.ReceiveActivity"android:label="@string/title_activity_receive" ><意图过滤器><action android:name="android.nfc.action.NDEF_DISCOVERED"/><category android:name="android.intent.category.DEFAULT"/><data 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) 当某个活动在前台时,您可以为 前台调度系统.然后,该活动可以忽略这些事件(它将在其 onNewIntent() 方法中接收.

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

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

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

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);}公共无效 onPause() {super.onPause();NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);nfcAdapter.disableForegroundDispatch(this);}公共无效 onNewIntent(意图意图){如果 (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 应用仅为一项活动启用 NFC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆