Android NFC启动屏幕
当我点击应用程序上的一个按钮时,我正试图阅读NFC标签。目前,我能够在默认模式下检测标签(Nexus phone中安装了标签应用程序)。但我无法显示要通过其启动标记的活动选择器Android NFC启动屏幕,android,broadcast,nfc,Android,Broadcast,Nfc,当我点击应用程序上的一个按钮时,我正试图阅读NFC标签。目前,我能够在默认模式下检测标签(Nexus phone中安装了标签应用程序)。但我无法显示要通过其启动标记的活动选择器 public class NFC_button extends Activity { protected IntentFilter ifilter ; private NfcAdapter adapter; private BroadcastReceiver receiver = new BroadcastRecei
public class NFC_button extends Activity
{
protected IntentFilter ifilter ;
private NfcAdapter adapter;
private BroadcastReceiver receiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()))
{
Parcelable[] messages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] ndefmessages;
if(messages != null)
{
ndefmessages = new NdefMessage[messages.length];
for(int i = 0;i<messages.length;i++)
{
ndefmessages[i] = (NdefMessage)messages[i];
}
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
adapter=NfcAdapter.getDefaultAdapter(this);
ifilter = new IntentFilter();
ifilter.addAction("android.nfc.action.NDEF_DISCOVERED");
ifilter.addCategory("android.intent.category.LAUNCHER");
}
@Override
protected void onResume() {
registerReceiver(receiver, ifilter);
super.onResume();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nfc.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.NFC"/>
<uses-feature android:name="android.hardware.nfc" android:required="true"/>
<uses-sdk android:minSdkVersion="10"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".NFC_ExampleActivity"
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=".NFC_button">
</activity>
</application>
公共类NFC_按钮扩展活动
{
受保护的IntentFilter ifilter;
专用NfcAdapter适配器;
private BroadcastReceiver=新的BroadcastReceiver()
{
@凌驾
公共void onReceive(上下文、意图)
{
if(NfcAdapter.ACTION\u NDEF\u DISCOVERED.equals(intent.getAction()))
{
Parcelable[]messages=intent.getParcelableArrayExtra(NfcAdapter.EXTRA\u NDEF\u messages);
NdefMessage[]ndefmessages;
如果(消息!=null)
{
ndefmessages=新的ndefmessages[messages.length];
对于(int i=0;i首先,我不认为BroadcastReceiver是读取标记的正确方法。我看到的另一个错误是您的意图过滤器有一个类别:
android.intent.category.LAUNCHER
但正确的分类应该是:
android.intent.category.DEFAULT
我建议您在触摸标记时将意图过滤器添加到要启动的活动清单中,如下所示:
<activity android:name=".NFC_button">
<intent-filter >
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
并将BroadcastReceiver的onReceive方法中的代码移动到NFC_按钮活动的onCreate
如果没有具体原因需要使用BroadcastReceiver,这将解决标记读取问题。首先,我认为BroadcastReceiver不是读取标记的正确方法。我看到的另一个错误是,您的意图过滤器有一个类别:
android.intent.category.LAUNCHER
但正确的分类应该是:
android.intent.category.DEFAULT
我建议您在触摸标记时将意图过滤器添加到要启动的活动清单中,如下所示:
<activity android:name=".NFC_button">
<intent-filter >
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
并将BroadcastReceiver的onReceive方法中的代码移动到NFC_按钮活动的onCreate
如果你没有使用广播接收器的特殊原因,这将解决你的标签阅读问题。
你能解释为什么你不认为广播接收器是正确的读取标签的方法吗?考虑一下我的用法:我正在写数据到标签。我希望用户准备数据,然后把标签放在靠近设备的数据T上。o在检测到标记时写入。我不想启动活动(更改UI)因为用户在另一个活动上,让他们的数据准备好写。你能解释为什么你不认为广播接收器是正确的读取标签的方法吗?考虑我的用法:我正在把数据写入标签。我希望用户准备数据,然后把标签放在设备附近,以便在标签被检测时写入数据。。我不想启动一个活动(更改UI),因为用户正在进行另一个活动,准备好写入数据。