使用NFC"有效载荷"变量"如果"声明

时间:2016-09-26 23:07:30

标签: android nfc external

我试图使用"有效载荷"来自我已写入NFC标签的外部记录的值。我知道该记录已成功写入NFC标签。我在使用有效载荷时遇到了问题" "如果"声明。我的代码如下:

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();
}

一旦我能够阅读"有效负载值,我想指定要运行的代码块。例如:

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"基于已写入标记的Payload值的语句。谢谢你的帮助!!

1 个答案:

答案 0 :(得分:0)

假设您在payload中引用字符串变量OnResume,那么您可以写:

if (payload.equals("1")) {...}