0
votes

I have an app that reads data from a NFC tag and prints it. Following this https://developer.android.com/guide/topics/connectivity/nfc/nfc.html#filtering-intents (see the section ACTION_TECH_DISCOVERED), I can start the app when I scan the NFC tag from android itself. However, this doesn't trigger the OnNewIntent method, that then parses and prints the data contained in the tag. It only works if I rescan the tag when the app is already open.

In mainActivity:

@Override
    protected void onNewIntent(Intent intent) {
        if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
          //stuff
        }
    }

In manifest:

<activity android:name=".MainActivity"
        <intent-filter>
            <action android:name="android.nfc.action.TECH_DISCOVERED" />
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data
            android:name="android.nfc.action.TECH_DISCOVERED"
            android:resource="@xml/filter_nfc"
        />
    </activity>

The question is: is it possible to trigger the OnNewIntent method directly, or is there another method?

Thank you

1
You also need to add your code in onCreate of your Activity/FragmentMustansir

1 Answers

0
votes

Try this:

protected void onCreate(Bundle savedInstanceState) {

//Your stuff
Intent intent = getIntent();
handleIntents(intent);
}

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        handleIntents(intent);
    }

private void handleIntents(Intent intent) {
 if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
          //stuff
        }
}