Android 在用户点击按钮后阅读nfc芯片
按下按钮后是否可以开始从nfc芯片读取数据。按下按钮后,会出现类似“请将您的nfc芯片靠近设备…”的信息。我只看到一些教程,这些教程展示了如何在将nfc芯片固定到设备(onNewIntent)上时启动应用程序 第二个问题。如果应用程序已在运行,而我将nfc芯片放在设备旁边,该怎么办?它是在强制摧毁然后再次发射吗Android 在用户点击按钮后阅读nfc芯片,android,nfc,Android,Nfc,按下按钮后是否可以开始从nfc芯片读取数据。按下按钮后,会出现类似“请将您的nfc芯片靠近设备…”的信息。我只看到一些教程,这些教程展示了如何在将nfc芯片固定到设备(onNewIntent)上时启动应用程序 第二个问题。如果应用程序已在运行,而我将nfc芯片放在设备旁边,该怎么办?它是在强制摧毁然后再次发射吗 谢谢 关于问题的第一部分,您可以在活动中使用一个标志来指示应用程序的状态(准备写入/消息正在显示,未准备写入/消息未显示)。您可以找到一个简单的示例: 关于问题的第二部分,当您希望在活动
谢谢 关于问题的第一部分,您可以在活动中使用一个标志来指示应用程序的状态(准备写入/消息正在显示,未准备写入/消息未显示)。您可以找到一个简单的示例:
关于问题的第二部分,当您希望在活动已在前台时接收NFC标记发现事件时,您应该向NFC前台调度系统注册。看见没有必要破坏和重新创建您的活动。已经实现了这两个答案!工作起来很有魅力!谢谢!
private static final int DIALOG_WRITE = 1;
private boolean mWrite = false; // write state
public void onCreate(Bundle savedInstanceState) {
[...]
// Set action for "Write to tag..." button:
mMyWriteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switch to write mode:
mWrite = true;
// Show a dialog when we are ready to write:
showDialog(DIALOG_WRITE);
}
});
[...]
}
protected Dialog onCreateDialog(int id, Bundle args) {
switch (id) {
case DIALOG_WRITE:
// A dialog that we show when we are ready to write to a tag:
return new AlertDialog.Builder(this)
.setTitle("Write to tag...")
.setMessage("Touch tag to start writing.")
.setCancelable(true)
.setNeutralButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int arg) {
d.cancel();
}
})
.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface d) {
mWrite = false;
}
}).create();
}
return null;
}
// You would call this method from onCreate/onStart/onResume/onNewIntent
// or from whereever you want to process an incoming intent
private void resolveIntent(Intent data, boolean foregroundDispatch) {
String action = data.getAction();
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
// The reference to the tag that invoked us is passed as a parameter (intent extra EXTRA_TAG)
Tag tag = data.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (mWrite) {
// We want to write
mWrite = false;
// TODO: write to tag
} else {
// not in write-mode
// TODO: read tag or do nothing
}
}
}