thomas.fogh
thomas.fogh

Reputation: 369

How to handle a continous HTTP connection from multiple Activities?

My app uses the Android XML-RPC project to communicate with a server. After the connection is established the app needs to keep the connection alive by sending a message to the server every xx second. The app also contains multiple Activities that need to send and receive messages using the connection.

What's the proper way to implement this?

Using a IntentService and BroadcastReceiver? Or just a Thread?

Upvotes: 0

Views: 295

Answers (1)

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

This looks like a perfect job for a Service started by the AlarmManager.

Your service in its onStart method will get whatever information you need for connection (like token, username, ...) from the preferences for instance. You can trigger the service start by using the AlarmManager sending on a regular basis an intent to start the service.

Another option would be to have a service started in the background, running a thread that does the communication each X seconds (use a sleep(delay) between calls.

Upvotes: 1

Related Questions