开发者

Getting progress of an IntentService

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜