简体   繁体   中英

Android How to magare Intentservice queue?

I have an activity with a download button. When i click on it a service is started. The service create a notification and post it in the notification bar. If i show the notification details i can see a progrees bar showing th download status.

Cause i didn't know that a service runs on the main thread, someone suggested me to use an intentservice that create a worker thread by itself. SO everything is fine and my gui doesn't freeze during the download.

I have one problem. To use intentservice i had to implement the download code and the notification part inside the method onHandleIntent. Now i found out that an intentservice has a kind of queue that manage intents once per time. SO if i click the download button from the activity twice it crate a new notification, starts the download and then when it has finished it enter onHandleIntent again,it creates the second notification and starts the second download. For me it's fine to download files back to back and not together, but at least i would like that if i press twice the download button the serivce upload the notifications saying that there are 2 downloads waiting. I'm not able to do that cause the intent service do the code once and the once again..........Is there a way to fix it?

Is there a way to handle the queue manually saying something like....if you get another intent do this........update notification.......and then continue with the dirst download....

ANother question, can a intent service modify the gui considering that is working on another thread or i have to use an handler and send a message back to the activity?

And last, whwere is the thread created, justa after the onCreated method or inside the onHandleIntent???

You could possibly save the number of download items in the SharedPreferences or in the Application class file ( both can be accessed globally ) and pass the number intent into the IntentService for the notification messages. Your Application class could have methods for updating download numbers (total, current file, etc). You could update this from the IntentService when it is destroyed or download complete and display in notification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM