tags:

views:

552

answers:

1

I have an android app that listens for json commands over a socket. I am wondering how I can implement this as a background service. The service would receive the commands and depending on which commands they are, notify the user, or update data within the main program. Some code examples would be great if anyone has them. I've got an example of how to build a background service however it uses timers. I'd like for socket to always be listening.

+2  A: 

In this case you would use a Service which responds to events sent over the socket.

The service should be started by the activity (could also be started on boot if needed).

For code example I would recommend commonwares projects on GitHub http://github.com/commonsguy Many great examples using Services there.

(there is also a service example in an ongoing app I have. Code link http://bit.ly/bkp7Pl)

dparnas
Perhaps someone can shed some light on what I've come up with so far. I've got a socket that waits for a message, and as of now i just want to display a toast message of that message. I have my code available at http://bit.ly/dwnreE . Background service is Background.java and the broadcast receiver is KegnetReceiever.java. As of now the the connector listen method works fine by itself, however i cant get it to work as a part of the service, or get a toast message to appear from a broadcast. thanks for the help!
Jared
been on vacation, therefore a bit slow to respond. Have you made any progress?At first view the code looks ok (although you are using C# type caps on your Java methods :) )Have you added some logs statements in order to verify that the service is started and the receiver has received events?(I personally use aLogCat app for viewing logs)
dparnas