<uses-feature android:name="android.hardware.nfc" android:required="true" />
<uses-permission android:name="android.permission.NFC" />
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/filter_nfc"
/>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- capture anything using NfcF -->
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
<tech>android.nfc.tech.MifareClassic</tech>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>
isNFC = getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC);
if (isNFC) {
mAdapter = NfcAdapter.getDefaultAdapter(this);
mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// Setup an intent filter for all MIME based dispatches
IntentFilter ndef = new IntentFilter(
NfcAdapter.ACTION_TECH_DISCOVERED);
ndef.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
try {
ndef.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
Log.e("NFCfail", "runtime Exception");
}
mFilters = new IntentFilter[] { ndef };
// Setup a tech list for all MifareClassic tags
mTechLists = new String[][] {
new String[] { MifareClassic.class.getName() },
new String[] { NfcA.class.getName() } };
}
protected void onResume() {
super.onResume();
if (isNFC && mAdapter.isEnabled()) {
mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
mTechLists);
}
}
感应卡之后在onNewIntent中获取到的数据如下:
public void onNewIntent(Intent intent) {
String action = intent.getAction();
// action打印输出为:android.nfc.action.TECH_DISCOVERED
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
// tagFromIntent 打印输出为:TAG: Tech [android.nfc.tech.NfcA]
问题是:
MifareClassic mfc = MifareClassic.get(tagFromIntent);
//这里mfc打印输出为null 这是为什么呢
}
谢谢!