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

标签 android nfc

是否可以在按下按钮后开始从 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/

相关文章:

android - 将原始数据写入 Android 串行端口 (USB)

android - NFC 标签必须是 NDEF 格式吗?

android - 在 NFC 操作模式之间进行选择

安卓 NFC : Reading tag does not work

java - NFC Acr122u-A2 读取

android - 有 libnfc android 可加载内核模块吗?

android - 如何在 Android 中将位图设置为通知图标

android - 当 TextView 中有多于一行输出时,Text 会触及 Parent 的开头

android - 膨胀异常 - 膨胀 TextView 时出错

android - android中的<include>和<ViewStub>标签有什么区别?