1. menifest文件中需要添加:
android:name="android.hardware.nfc"
android:required="true" />
在activity中添加:
2. 初始化时代码:
Intent nfcIntent = new Intent(this, getClass());
nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mPendingIntent =
PendingIntent.getActivity(this, 0,nfcIntent , 0);
mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter == null) {
Toast.makeText(getApplicationContext(), "NFC feature is supported on this device.", Toast.LENGTH_SHORT).show();
return;
}
需要Override的函数:
private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;
@TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
protected void onResume() {
super.onResume();
mAdapter.enableForegroundD