使用NFC的“有效负载" "if"中的变量陈述 [英] Use NFC "payload" variable in "if" statement
问题描述
我正在尝试使用已写入NFC标签的外部记录中的有效负载"值.我知道记录已成功写入NFC标签.我在"if"语句中使用该有效载荷"值时遇到问题.我的代码如下:
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);
}
}
我希望这是有道理的,我只是试图根据已写入标记的有效负载的值运行一个简单的"if/else"语句.谢谢您的帮助!
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!!
推荐答案
假定您在OnResume
中引用了字符串变量payload
,则可以编写:
Assuming that you refer to the String variable payload
in OnResume
, then you can write:
if (payload.equals("1")) {...}
这篇关于使用NFC的“有效负载" "if"中的变量陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!