检查应用是否已通过NFC标签启动

时间:2017-04-04 19:12:15

标签: android nfc

有一种方法可以了解应用是否已通过NFC标签启动了吗?

我编写了一个nfc标签,用于启动我的应用程序编写应用程序包名称(使用第三个应用程序),我想在这种情况下启动一个特定的片段,我该怎么做?

1 个答案:

答案 0 :(得分:0)

您必须为应用的活动添加一个意图过滤器,该活动会注册由nfc事件启动的活动。基于mime类型或技术等,有不同的方法来指定过滤器。 然后在该活动中,您可以在onNewIntent()onResume()中检查启动该活动的意图是否为nfc意图。

public void onNewIntent(Intent newIntent) {
    setIntent(newIntent);
    // onResume is called afterwards, so we handle intent there
}

public void onResume() {
    Intent intent = getIntent();
    if (intent != null && NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Toast.makeText("NFC!", Toast.LENGTH_LONG).show();
        // Here you could start your Fragment
    }
}

请查看有关意图过滤器等选项的官方文档。

https://developer.android.com/guide/topics/connectivity/nfc/nfc.html

相关问题