質問

In my application I create a download queue via IntentService upon receiving sms, I've got that to work like a charm.

sms received -> Smsreceiver extends BroadcastReceiver -> startService(intent)
-> SimpleIntentService extends IntentService //actual download happens here
-> sendBroadcast(broadcastIntent) -> back to my app //to inform that
                                                   //a download has finished

But a creepy case rises when the device is offline, I really don't know what to do there, shall I hold on the currently being executed thread until the device gets online again (which is high cost process in my opinion), or shall i build my own queue that holds urls and feeds IntentService when back online? or any other suggestion?

Also how to check when device is online again without affecting device performance/battery/etc ?

役に立ちましたか?

解決

I would tell you ....

1- When you receive an SMS, add something to your queue

2- In your service :

  • Check your connectivity

    • If your online : Get Queue list

    For each action, do whatever you want to do and delete it from your queue list

    • If you're not : Do nothing

3- Use CONNECTIVITY_CHANGE broadcast to check connectivity change and if you are connected. In this case,reproduces STEP 2. Else do nothing


Check http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#MonitorChanges for monitoring change of connectivity (broadcast section)

他のヒント

If user is currently offline you will need to store the current state/data. This you can do either using database or SharedPrefernces. It depends on the complexity of your requirement which one to use.

For checking wwhen the network is online/offline you will need to listen to android.net.conn.CONNECTIVITY_CHANGE refer to link - Check INTENT internet connection

I'm not sure if it's the best practice or not, but I think you can combine your queue idea with Google Cloud Messaging.

Whenever an sms is received, add it to queue and send it to your Application Server. Then application server sends "download this" message to your device. Your device will get gcm push notification iff it is online, hence it can start download. After download finished, perform same steps for next element in queue.

I think this method solves the problem of polling for internet connection

Square wrote the Tape library for this scenario, as a persistent queue that they can start reading from when connectivity returns.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top