Intent.hasExtra(NfcAdapter.EXTRA_TAG)返回null

时间:2016-08-21 06:23:17

标签: android nfc onnewintent

我正在尝试在我的活动中阅读NFC标记(这不是主要活动)。一切正常,只是因为这不能读取"NfcAdapater.EXTRA_TAG",而这需要获得标记text的{​​{1}}。

活动

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");
    }
}

0 个答案:

没有答案