我正在尝试阅读NFC
我的活动的标签(这不是主要活动)。一切正常,只是这不读取"NfcAdapater.EXTRA_TAG"
,这是获取text
标签所必需的NFC
。
活动
private IntentFilter[] intentFilters = new IntentFilter[]{};
...
@Override
protected void onResume() {
super.onResume();
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0,new Intent(this, getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
adapter.enableForegroundDispatch(this, pendingIntent, intentFilters, null);
}
@Override
protected void onPause() {
super.onPause();
adapter.disableForegroundDispatch(this);
}
@Override
protected void onCreate(Bundle ssavedInstanceState){
...
adapter = NfcAdapter.getDefaultAdapter(this);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
String action = intent.getAction();
//This is the intent action: android.nfc.action.NDEF_DISCOVERED
Log.e("NFC", "new intent nfc | " + intent.getType());
/*This is the trouble->*/ if (intent.hasExtra(NfcAdapter.EXTRA_TAG)) {
Log.e("extra tag", "si");
Parcelable[] parcelables = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if(parcelables != null && parcelables.length > 0) {
Log.e("parcelable", "si");
readTextFromMessage((NdefMessage) parcelables[0]);
}else{
Log.e("parcelabe", "no");
Toast.makeText(this, "No NDEF messages found!", Toast.LENGTH_SHORT).show();
}
}else{
Log.e("extra tag", "no");
}
}
显现:
<?xml version="1.0" encoding="utf-8"?>
...
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
...
<activity
android:name=".nfc.ui.NfcActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
...