1

I am doing a NFC work in my activity. In onResume method of my activity, Activity waits that another NFC device comes closer for interaction. I want to implement this with a service. I want my service to wait for an ACTION_NDEF_DISCOVERED tag and when the tag comes, get the information which will be sent from another device and send it to my activity.

Thanks for your helps.. Have a nice day

akbas
  • 175
  • 4
  • 14
  • You mean, in onResume() your activity starts waiting? That can't be right. I assume the onResume() just finishes and then nothing happens? You have an intent filter on your service, for the nfc intent? And your activity has given the service a callback so it can notify the activity of an nfc discovered? Then, what's your question? – Christine Jun 13 '12 at 08:03
  • my question is where to write that intent filter.. When that tag is discovered by the service, the service will bring the activity front. I asked nearly the same before.. Can you check this link please? http://stackoverflow.com/questions/10980315/onresume-method-for-android-services thanks for your helps already – akbas Jun 13 '12 at 08:23

1 Answers1

1

Only activities (running in the foreground) can receive NFC intents and communicate over NFC. Services do not have this capability.

NFC guy
  • 10,151
  • 3
  • 27
  • 58
  • for example, can I write if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) this code into my service to be able to bring my activity front? I can do bringing activity to front. I want to trig it with NFC – akbas Jun 13 '12 at 15:07
  • I don't think that condition will ever be true. Why not just receive the NFC intent in your activity directly? That is how it is intended to work. – NFC guy Jun 13 '12 at 15:34
  • I want that my application does NFC interaction even it is not foreground application. I thought that the service will provide me that – akbas Jun 14 '12 at 07:45
  • No, that's what the NFC intent filtering is for, see http://developer.android.com/guide/topics/nfc/nfc.html#filtering-intents. It will bring your app automatically to the foreground, where it e.g. set up communication with the tag. – NFC guy Jun 14 '12 at 18:42
  • Thanks again for your helps.. I want to bring my app to foreground when it is in the background with NFC tag. it doesn't matter with NDEF or TAG. can you send me a code sample? couldn't understand it from the website:/ – akbas Jul 05 '12 at 07:00
  • You don't really need code; you need XML. Add one of the example intent filters at http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#filtering-intents to your manifest. – NFC guy Jul 05 '12 at 19:34
  • for example I added android.nfc.action.TAG_DISCOVERED.. What should I do to bring my activity to front? Thanks for your helps by the way – akbas Jul 05 '12 at 21:09
  • TAG_DISCOVERED has the lowest priority, see http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#dispatching for details. Better use NDEF_DISCOVERED if you can (does your tag contain NDEF data?) or otherwise TECH_DISCOVERED (see http://developer.android.com/reference/android/nfc/NfcAdapter.html#ACTION_TECH_DISCOVERED on how to set up the meta-data XML file). When Android decides that your app is the one that matches, it will bring the proper Activity to the front automatically. – NFC guy Jul 06 '12 at 14:32
  • ok. I added this to my manifest file. Now you say,that my application will come front by itself. Should I write anything to onresume or to onpause method anything for this? – akbas Jul 06 '12 at 14:51
  • If your tag contains a plain text NDEF message (either NFC Forum Well-Known Type "T" or MIME type "text/plain"), your app will be shown when you tap the tag. If you want to know details about your tag, try an app like [TagInfo](https://play.google.com/store/apps/details?id=com.nxp.taginfolite) – NFC guy Jul 06 '12 at 20:07
  • so I do not need to add anything to my code, if I have those things in my manifest. I will try it and let you know if it works. Thanks for your helps al ready – akbas Jul 06 '12 at 20:54