简体   繁体   中英

Check if the app has been started by a NFC tag

There is a way to understand if the app has been started by a NFC tag?

I have programmed a nfc tag for launching my app writing the app package name (with a third app) and i want to start a specific fragment in that case, how can i do that?

You have to add an intent filter to an activity of your app, which registers that activity to be started by an nfc event. There are different ways to specify that filter, based on mime-type or technology etc. Then in that activity, you can check in onNewIntent() and in onResume() if the intent that started the activity was an nfc intent.

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

Please check the official documentation about the options you have for intent filters etc.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM