androidandroid-studionfcndefcardreader

When NFC reads the card, it opens the application by itself, how can I solve this problem?


When NFC reads the card, it opens the application by itself but I want to read NFC only when I open this application

 <activity
            android:name="MyActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <meta-data
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
        </activity>

nfc_tech_intent_filter

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.IsoDep</tech>
    </tech-list>
</resources>

Solution

  • The answer is don't put the intent-filter and tech-filter in the manifest, this is what is causing the NFC service to open your application when it reads a matching NFC card.

    so remove from the manifest

    <intent-filter>
                    <action android:name="android.nfc.action.TECH_DISCOVERED" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
                <meta-data
                    android:name="android.nfc.action.TECH_DISCOVERED"
                    android:resource="@xml/nfc_tech_filter" />
    

    Just use nfcAdapter.enableForegroundDispatch or even better nfcAdapter.enableReaderMode API's in your activities onResume (and the disabled methods in onPause) to read NFC cards when you App is already running and in the foreground