gpt4 book ai didi

android - 用户点击按钮后读取 nfc 芯片

转载 作者:行者123 更新时间:2023-11-30 02:58:02 30 4
gpt4 key购买 nike

是否可以在按下按钮后开始从 nfc 芯片读取数据。按下按钮后,应该会出现一条消息,例如“请将您的 nfc 芯片靠近设备……”。我只看到教程,它展示了如何在设备上拿着 nfc 芯片时启动应用程序 (onNewIntent)。

第二个问题。如果应用程序已经在运行并且我将 nfc 芯片放在我的设备旁边怎么办?是否强制销毁然后再次发射?

谢谢!

最佳答案

关于您问题的第一部分,您可以在您的 Activity 中使用一个标志来指示您的应用程序的状态(准备好写入/正在显示消息,未准备好写入/消息未显示)。你可以找到一个简单的例子here :

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
}
}
}

关于你问题的第二部分,如果你想在你的 Activity 已经在前台时接收 NFC 标签发现事件,你应该向 NFC 前台调度系统注册。参见 Advanced NFC: Using the NFC Foreground Dispatch System .无需销毁和重新创建您的 Activity 。

关于android - 用户点击按钮后读取 nfc 芯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22964166/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com