开发者

Getting progress of an IntentService

开发者 https://www.devze.com 2023-04-11 17:47 出处:网络
I\'m working on an Android app that needs a lot of API calls. I\'ve build a system implementing a queuing system based on an IntentService. The thing is, I\'d like to show an accurate ProgressBar upda

I'm working on an Android app that needs a lot of API calls. I've build a system implementing a queuing system based on an IntentService. The thing is, I'd like to show an accurate ProgressBar updating while the IntentService is running.

One way would be to change the queuing system to a serie of AsyncTasks. I would have to show the progress using onProgressUpdate().

Another way would be to tweak the IntentService and get the progress from a background thread.

The thing is, I'd like to combine IntentService and AsyncTask nice features: make a queuing system that handles easily UI events. Is there a best way to implement that?

EDIT: I've ended up using AsyncTasks instead of IntentServices. But开发者_Go百科 still, if anyone has a nice pattern to share to update progress from an IntentService, feel free to add your comment.


I'd

  • start a HandlerThread and a Handler
  • Get a handler for the main UI thread
  • post work to the background thread using that thread's handler
  • post progress updates and results to the UI using the UI thread's handler

That way you can have a single background thread to handle your work queue, and post anything you like back to the UI thread without dealing with broadcasts


With me, for an IntentService, I was thinking of broadcasting when to start the progressbar, and when to end the progressbar. Usually, you can use the standard "receive back" for the end part. You can register different intent broadcasts with different strings.

The tricky part was to figure out a way for the call to find out if the Progress bar is cancelled, and I was thinking of having a status table that can get updated as the REST call progresses. The table will have an id of the call, and after it's done, the entry gets deleted. The table will keep track if the thread should be active.

So, the REST thread will check the table before each send, and stop everything / ignore sending back the data if it detects a cancellation

0

精彩评论

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

关注公众号