I'm attempting to use the "payload" value from an external record that I've written to an NFC tag. I know that the record has been written successfully to the NFC tag. I'm having problems using that "payload" value in an "if" statement. My code follows:
protected void onResume() {
super.onResume();
// read nfc tag...IS THIS WHAT WILL "READ" THE PAYLOAD VALUE THAT HAS BEEN WRITTIEN?
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();
}
Once I'm able to "read" the payload value, I want to specify what block of code to run. For example:
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
///IF Payload Value = 1...Then run this code block:
if (intent.hasExtra(NfcAdapter.EXTRA_TAG))
{
Toast.makeText(this, "NFC Scan", Toast.LENGTH_SHORT).show();
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
///IF Payload = 2, then run this code block:
if (intent.hasExtra(NfcAdapter.EXTRA_TAG))
{
Toast.makeText(this, "NFC Scan", Toast.LENGTH_SHORT).show();
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String url = "http://www.yahoo.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
}
I hope this makes sense, I'm only trying to run a simple "if/else" statement based on the value of the Payload that has been written to the tag. Thanks for any help!!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…