我正在使用以下代码“读取” nfc标签:

@Override
protected void onResume () {
    super.onResume();

    // read nfc tag....this is what will "read" the external record?
    if (getIntent().hasExtra(NfcAdapter.EXTRA_TAG)) {

        NdefMessage ndefMessage = this.getNdefMessageFromIntent(getIntent());

        if(ndefMessage.getRecords().length > 0){

            NdefRecord ndefRecord = ndefMessage.getRecords()[0];

            String payload = new String(ndefRecord.getPayload());

            Toast.makeText(this, payload, Toast.LENGTH_SHORT).show();

        }

    }


    enableForegroundDispatchSystem();

}


我收到一个错误消息,说它“无法解析方法getNdefMessageFromIntent”,还有其他方法我可以用来“读取”标签上的有效载荷吗?不确定如何解决此问题。谢谢您!

最佳答案

您可以尝试以下方法:

if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) { // NDEF Message available
    NdefMessage msg =(NdefMessage) rawMsgs[0];
    NdefRecord ndefRecords = ndefMessage.getRecords();

    String payload = new String(ndefRecord[0].getPayload());
    Toast.makeText(this, payload, Toast.LENGTH_SHORT).show();
}


}

10-04 12:00