Android应用程序启用NFC只为一个活动 [英] Android app enable NFC only for one Activity

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

问题描述

是否有可能启用NFC的只有一个机器人为具有NFC功能的应用程序的活动?

我读过这一点, <一href="http://stackoverflow.com/questions/9748513/reading-nfc-tags-only-from-a-particuar-activity">Reading NFC标签只能从particuar活动

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

编辑:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.nfccheckout&GT;

    &LT;用途特征
        机器人:名称=android.hardware.nfc
        机器人:要求=真/&GT;

    &LT;使用-权限的Andr​​oid:名称=android.permission.NFC/&GT;

    &lt;应用
        机器人:allowBackup =真
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme&GT;
        &LT;活动
            机器人:名称=。activities.MainActivity
            机器人:标签=@字符串/ APP_NAME&GT;
            &LT;意向滤光器&gt;
                &lt;作用机器人:名称=android.intent.action.MAIN/&GT;
                &LT;类机器人:名称=android.intent.category.LAUNCHER/&GT;
            &所述; /意图滤光器&gt;
        &LT; /活性GT;
        &LT;活动
            机器人:名称=。activities.ReceiveActivity
            机器人:标签=@字符串/ title_activity_receive&GT;
            &LT;意向滤光器&gt;
                &lt;作用机器人:名称=android.nfc.action.NDEF_DISCOVERED/&GT;
                &LT;类机器人:名称=android.intent.category.DEFAULT/&GT;
                &lt;数据机器人:MIMETYPE =应用/ JSON + com.example.nfccheckout/&GT;
            &所述; /意图滤光器&gt;
        &LT; /活性GT;
        &LT;活动
            机器人:名称=。activities.CreatePayloadActivity
            机器人:标签=@字符串/ title_activity_create_payload&GT;
        &LT; /活性GT;
        &LT;活动
            机器人:名称=。activities.ConfirmationActivity
            机器人:标签=@字符串/ title_activity_confirmation&GT;
        &LT; /活性GT;
    &LT; /用途&gt;

&LT; /舱单&GT;
 

解决方案

如果你想魔鬼的NFC发现事件(处理 NDEF_DISCOVERED TECH_DISCOVERED TAG_DISCOVERED ),而有一定的活动是在前台,你会注册该活动的<一个href="http://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html#foreground-dispatch"相对=nofollow>前台调度系统。这项活动就可以忽略这些事件(这将接受在其 onNewIntent()方法。

这将prevent NFC发现事件被传递到的任意的其他活动(所以那些在你的应用程序和任何其他已安装的应用程序)具有在清单中注册的NFC disovery意图过滤器

不过,这种方法的不可以禁用设备的NFC调制解调器。因此,NFC芯片将仍然轮询标签,但他们只是没有报任何应用程序。

所以,要禁用NFC的会做这样的事情的所有活动:

 公共无效onResume(){
    super.onResume();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(本);
    PendingIntent pendingIntent = PendingIntent.getActivity(此,0,新意图(本,的getClass())addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
    nfcAdapter.enableForegroundDispatch(这一点,pendingIntent,NULL,NULL);
}

公共无效的onPause(){
    super.onPause();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(本);
    nfcAdapter.disableForegroundDispatch(本);
}

公共无效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天全站免登陆