1

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>
mervegenc
  • 13
  • 5

1 Answers1

1

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

Andrew
  • 8,198
  • 2
  • 15
  • 35
  • very thanks, this solution worked for me, when I removed intent filter and tech- filter in the manifest so this trouble was solved by you, again very thanks. – mervegenc Oct 08 '20 at 18:12