bytebiscuit
bytebiscuit

Reputation: 3496

stopping/clearing IntentService queue when Internet is down?

I'm loading an array of files to be downloaded and i'm starting an IntentService for each of those files. The problem is in the case of Internet connection problems such as if the device is disconnected I will have to somehow clear or stop the IntentService's queue processing.

In my code I have a while loop in which the file is being downloaded. One of the conditions for this loop to run is: checking for Internet Connectivity which I have done using the following code:

public boolean isOnline() {
         ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
         if(cm != null){
             return cm.getActiveNetworkInfo().isConnectedOrConnecting(); 
         }
         return false;


 }

my loop is similiar to the following:

while(.. , isOnline()){
    /// Download file
}

If I'm now downloading a file in the queue and if the Internet goes down the loop will not run anymore and the IntentService for that file will stop but now if the internet comes back the download will start from the next file in the queue ignoring the last file which we couldn't download. How can I clear the IntentService queue or make the IntentService queue resume the download from where it left.

Upvotes: 1

Views: 1999

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006869

How can I clear the IntentService queue or make the IntentService queue resume the download from where it left.

You can't really do either, AFAIK. The queue used by IntentService is not exposed to the developer.

In this case, you may be better served creating your own queue (e.g., LinkedBlockingQueue), your own thread monitoring that queue, and your own Service wrapping up all of that (and handling the case where, once the queue empties, you call stopSelf() and exit the background thread).

Or, have the IntentService notify something (e.g., via a Messenger) about the failed download, so you can enqueue that file sometime later once Internet access returns.

Upvotes: 1

Related Questions