How to receive NFC tag data with my activity not foregound

824 views Asked by At

I'm developing a NFC application to turn WiFi on and connect to some specific network when NFC tag is tapped. So far, I've understood that NFC activity should be on foreground to get NFC data from Android system. But, I doubt whether my understanding is right or not, since I've seen the operation of NFC Tasks(https://play.google.com/store/apps/details?id=com.wakdev.nfctasks). Although the application is not activated on receiving NFC data, It is launching the corresponding task and turning WiFi on successfully.

I'd like to know how it is possible. Is there anyone who can explain its operation?

Thanks in advance.

Logcat on tapping NFC Tag made by 'NFC Tools' : I/NfcDispatcher(822): matched AAR to NDEF : E/SoundPool(822): Don`t play soundpool when NOT normal mode : E/NfcService(822): applyAlternativeHandler::Unknown message received : I/ActivityManager(553): START {act=android.nfc.action.NDEF_DISCOVERED typ=w1/33 pkg=com.wakdev.nfctasks cmp=com.wakdev.nfctasks/.NFCActivity (has extras) u=0} from pid 822

Logcat on tapping NFT Tag made by my application : I/NfcDispatcher(822): matched AAR to application launch : E/SoundPool(822): Don`t play soundpool when NOT normal mode : E/NfcService(822): applyAlternativeHandler::Unknown message received : I/ActivityManager(553): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.Farraf.DigitalSignage cmp=com.Farraf.DigitalSignage/.main.DigitalSignageActivity u=0} from pid 822 : I/ActivityManager(553): Start proc com.Farraf.DigitalSignage for activity com.Farraf.DigitalSignage/.main.DigitalSignageActivity: pid=10160 uid=10008 gids={3003, 1015, 1028}

1

There are 1 answers

4
Michael Roland On

Besides receiving NFC tag events trhough the foreground dispatch system (see Foreground Dispatch System), you can register your activities to be launched upon and receive NFC events through the AndroidManifest.xml of your app (see Filtering for NFC Intents). E.g. if your NFC tag contains the URI http://www.example.com/, you could create an intent filter like this:

<activity ...>
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:scheme="http"
              android:host="www.example.com" />
    </intent-filter>
</activity>