开发者

android : communication between service running in background and activity

I have a service running in the background. Based on some condition it has to start some activit开发者_如何学Goy. Activity has to send back the response.

I did google search and found out we have to use Notification mechanism. But I am not clear how to send the response back from activity to the service running.

Also service is collecting sensor data(acclerometer, gps). So should activity be started in separate thread so that collecting sensor data is not affected.

Please clarify.


Activity would be started in main UI thread, instead, your long-running service should run in and manage its own thread, since according to the document, service is also created in main thread.

Basically the best way to communicate is to use Intent. This allows loose couple of sender/receiver (i.e., activity/service in your case). Intent is a large topic in terms of android, and yet it is one of the most fundamental one, I think you should look for tutorials online about it.


If your activity is opened and return the result after completing the task of Activity then you can use startActivityForResult and then return the result.

Using of notification is simple, just create a interface class, and implement the class in your service. When you need to send data back to service, you can just call the appropriate method with data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜