【发布时间】:2014-04-25 19:58:16
【问题描述】:
我有一个使用 ndef 数据格式读取和写入 nfc 标签的应用程序。这一切似乎都还好。但每当我试图让标签靠近我的手机时,它就会产生一个新的活动。我只想在不打开新意图的情况下获取标签上的数据。所以我可以让我的应用决定标签是否连续被点击了两次。
我处理即将到来的标签的代码:
public class MainActivity extends Activity {
public static Context myContext;
private Button myButton;
private int currentID, currentBalance;
protected void onCreate(Bundle savedInstanceState) { // Firstly Created when
// a tag tapped to
// the phone
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button) findViewById(R.id.button1);
currentID = 0;
currentBalance = 0;
myButton.setOnClickListener(new View.OnClickListener() { // Tag write
// function
// places
// here
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), nfcWrite.class);
i.putExtra("id",currentID);
i.putExtra("balance",currentBalance);
startActivity(i);
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Intent intent = getIntent();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefRecord myRecord = ((NdefMessage) rawMsgs[0]).getRecords()[0];
String nfcData = new String(myRecord.getPayload());
String[] splitData = nfcData.split("-");
currentID = Integer.parseInt(splitData[0]);
currentBalance = Integer.parseInt(splitData[1]);
Toast.makeText(getApplicationContext(), nfcData, Toast.LENGTH_LONG).show();
}
}
}
【问题讨论】:
标签: android android-intent nfc