第一步添加权限: <uses-permission android:name="android.permission.NFC" /> <uses-permission android:name="android.hardware.nfc" android:required="true" />
第二步:在需要读取卡内容的地方写下面的内容
@SuppressLint("NewApi") @Override protected void onResume() { super.onResume(); NfcUtils.init(this); if (NfcUtils.mNfcAdapter != null) //监听NFC设备 NfcUtils.mNfcAdapter.enableForegroundDispatch(this, NfcUtils.mPendingIntent, NfcUtils.mIntentFilter, NfcUtils.mTechList); } @SuppressLint("NewApi") @Override protected void onPause() { super.onPause(); if (NfcUtils.mNfcAdapter != null) NfcUtils.mNfcAdapter.disableForegroundDispatch(this); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); try { //hyl 2019.3.1 NFC 添加使用数据区数据 此数据为二维码数据 String data = NfcUtils.readNFCFromTag(intent); Log.e(TAG, "onNewIntent: " + data); if (data == null&&data.isEmpty()) { Toast.makeText(this,"扫码失败",Toast.LENGTH_SHORT).show(); return; } else { //获取卡里的数据的逻辑处理 String[] split = DesUtils.decrypt(data).split(","); mUpLoad.setText(split[0]); } } catch (UnsupportedEncodingException e) { e.pr