【发布时间】:2012-03-20 09:07:45
【问题描述】:
当我单击应用程序上的按钮时,我正在尝试读取 NFC 标签。目前我能够在默认模式下检测到标签(标签应用程序安装在 Nexus 手机中)。但我无法显示我想通过它启动我的标签的活动选择器
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>
【问题讨论】: