检查应用程序是否已通过 NFC 标签启动
Check if the app has been started by a NFC tag
有没有办法了解应用程序是否已通过 NFC 标签启动?
我已经编写了一个 nfc 标签来启动我的应用程序并写入应用程序包名称(使用第三个应用程序),我想在这种情况下启动一个特定的片段,我该怎么做?
您必须向应用程序的 activity 添加一个 Intent 过滤器,它注册 activity 以通过 nfc 事件启动。根据 mime 类型或技术等,有不同的方法来指定该过滤器。
然后在 activity 中,如果启动 activity 的意图是 nfc 意图,则可以在 onNewIntent()
和 onResume()
中检查。
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
}
}
请查看官方文档,了解有关 Intent 过滤器等选项的信息。
https://developer.android.com/guide/topics/connectivity/nfc/nfc.html
有没有办法了解应用程序是否已通过 NFC 标签启动?
我已经编写了一个 nfc 标签来启动我的应用程序并写入应用程序包名称(使用第三个应用程序),我想在这种情况下启动一个特定的片段,我该怎么做?
您必须向应用程序的 activity 添加一个 Intent 过滤器,它注册 activity 以通过 nfc 事件启动。根据 mime 类型或技术等,有不同的方法来指定该过滤器。
然后在 activity 中,如果启动 activity 的意图是 nfc 意图,则可以在 onNewIntent()
和 onResume()
中检查。
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
}
}
请查看官方文档,了解有关 Intent 过滤器等选项的信息。
https://developer.android.com/guide/topics/connectivity/nfc/nfc.html