开发者

Android: How to Update in Background and Provide an Optional Progress Bar

My app n开发者_如何转开发eeds to download large xml files in the background and then use them to update a database. I would like these updates to happen automatically in the background, without bothering the user. This part seems easy and it seems like AsyncTask is what I'd use here.

However, I also want to provide a way for the user to:

1) Manually start an update if and only if the app is not already updating

2) View the status of an update already in progress

Here is the flow of the app as I see it now:

1) App is launched, main Activity is started

2) AsyncTask is automatically started by main Activity to update app

3) User selects "Update" from main menu

4) User is presented with the Update Activity

5) If update is going on in background:

--5A) User sees a Progress Bar

--5B) Otherwise, user sees option to start update

I think step 4 to 5 is where my main problem is. The Update Activity is a new Activity, different than the main Activity. I don't know how to access the AsyncTask from the Update Activity.

In other words, I start AsyncTask in one Activity, but I only want AsyncTask to display its progress in a second Activity, which may be open and closed at any time.

Some options I've been exploring include Service and BroadcastReceiver, but I don't know enough about either to make a good decision.

Thanks for your help.


Okay, after much reading and testing, here's how I ended up doing it.

I have a Service (UpdateService) that contains an AsyncTask. When the app is started, UpdateService is started automatically as well. If an update is not needed, UpdateService stops. If an update is needed, UpdateService lets its AsyncTask download the file and update the database.

At any time, the user can choose to view an update page, which is an extension of Activity (UpdateActivity). When UpdateActivity is invoked, it binds to UpdateService. Keep in mind, UpdateService only does any work when it is started (OnStartCommand()), not when it is bound (onBind()).

UpdateActivity implements a custom interface called ProgressListener, which contains a single method onProgressUpdate(). UpdateService can accept a ProgressListener.

When UpdateActivity binds to UpdateService, two things happen. 1) UpdateActivity sends a reference of itself to UpdateService as a ProgressListener. 2) The status of AsyncTask is checked. If AsyncTask has a status of Status.RUNNING, a progress bar is made visible for UpdateActivity's view. Otherwise, a button ("Update Now") is made visible. Clicking the button also makes the progress bar visible and starts UpdateService (onStartCommand()).

As AsyncTask performs the update in the background, it calls publishProcess() to send progress reports back to UpdateService. If a ProgressListener (UpdateActivity) is registered, UpdateService then in turn calls onProgressUpdate() to send progress reports to that UpdateActivity, which then updates the progress bar.

The user may enter and exit UpdateActivity at any time and the progress bar will be displayed mostly as expected.

I don't know if this is the best way to achieve this effect, but it works for me for now.

Thanks for the pointers schwiz.


I think looking into a service would be a good call here. Perhaps you could use the notification API to display the progress. You may not even need the second activity in that case. The user hits the update button and now the notification bar has the progress and will continue to function if the user exits the app.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜