是否有可能启用NFC的只有一个机器人为具有NFC功能的应用程序的活动?
我读过这一点, Reading NFC标签只能从particuar活动
但设备仍然在扫描标签上的应用程序的所有活动。
编辑:
< XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.nfccheckout>
<用途特征
机器人:名称=android.hardware.nfc
机器人:要求=真/>
<使用-权限的Android:名称=android.permission.NFC/>
<应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名称=。activities.MainActivity
机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<活动
机器人:名称=。activities.ReceiveActivity
机器人:标签=@字符串/ title_activity_receive>
<意向滤光器>
<作用机器人:名称=android.nfc.action.NDEF_DISCOVERED/>
<类机器人:名称=android.intent.category.DEFAULT/>
<数据机器人:MIMETYPE =应用/ JSON + com.example.nfccheckout/>
&所述; /意图滤光器>
< /活性GT;
<活动
机器人:名称=。activities.CreatePayloadActivity
机器人:标签=@字符串/ title_activity_create_payload>
< /活性GT;
<活动
机器人:名称=。activities.ConfirmationActivity
机器人:标签=@字符串/ title_activity_confirmation>
< /活性GT;
< /用途>
< /舱单>
解决方案
如果你想魔鬼的NFC发现事件(处理 NDEF_DISCOVERED
, TECH_DISCOVERED
, TAG_DISCOVERED
),而有一定的活动是在前台,你会注册该活动的前台调度系统。这项活动就可以忽略这些事件(这将接受在其 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
}
}