开发者

Sending intents from receiver to service with a delay

I have a Receiver for BOOT_COMPLETED intent and it will start a Service by calling startService with an intent and the Service processes that i开发者_如何学Pythonntent in onStartCommand function by spawning a thread that does a HTTP post to a server.

On powerup, sometimes it takes 3 to 5 seconds to get the active data connection on phone, when this happens Service fails to do HTTP post as there is no data connection. Is it possible for the Receiver to send the intent to Service using startService with a delay.? so that when onStartCommand of Service is called data connection is ready to post data.


Can't you use an asynctask to do the HTTP request and then maybe catch an exception within the asynctask to check for a timeout exception?

If the asynctask times out then return gracefully and make your service spawn a new asynctask until you get a connection?


Before doing any data communication, make a loop which will poll Android's TelephonyManager.getDataState() with exponential backoff to not use up too much CPU or waste the user's battery. Keep trying until you get the DATA_CONNECTED flag, or until you reach some sort of timeout to stop trying.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜