开发者

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 s开发者_运维问答end and receive messages using the connection.

What's the proper way to implement this?

Using a IntentService and BroadcastReceiver? Or just a Thread?


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜