Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android activity - remote service aidl two way connection

Tags:

android

I have two problems:

  1. I know that for connection activity and remote-service I have to use AIDL. I tried this and it's work but I can find only one way connections example. In simple words - reading something from service (by activity). But I need solve for sending some data to activity (by or from service). It's so important because the service have to send some information to activity immediatly after some its events (obtain data from the net).
  2. Is it way to bring to front again closed application (activity) from the remote service?

Any suggestions would be greatly appreciated.

Regards

Artik

like image 257
Artik Avatar asked Nov 11 '12 12:11

Artik


2 Answers

It's so important because the service have to send some information to activity immediatly after some its events (obtain data from the net).

You can use AIDL for two-way communication. You would need to expose not only the service interface, but a callback interface, via AIDL, with the client having the .Stub of the callback and supplying an instance of it in a parameter to a method on the service interface. This gets a bit complex -- here are a pair of sample apps from my book that demonstrate the technique:

  • Service
  • Client

Is it way to bring to front again closed application (activity) from the remote service?

Your service can call startActivity(), but generally that is a bad idea. The user may be in the middle of doing something else, when all of a sudden your activity pops into the foreground. Occasionally, the user may deem your activity to be more important, but not always. Consider using a Notification instead, to let the user know that there is something in your app that needs the user's attention.

like image 87
CommonsWare Avatar answered Nov 15 '22 15:11

CommonsWare


First, create a private resultreceiver variable in your service. Then create a method to set this resultreceiver via a connected activity. Then use AIDL to pass on a resultreceiver to the running service from the activity via the method you just made. Then in the service use resultreceiver.send if the resultreceiver is not null.

A few examples to get you started http://lalit3686.blogspot.com/2012/06/how-to-update-activity-from-service.html?m=1

http://chrisrisner.com/31-Days-of-Android--Day-28–Intents-Part-3--Service-Intents

like image 34
Harsha Alva Avatar answered Nov 15 '22 14:11

Harsha Alva