Update progressbar from AsyncTaskLoader?

Warpzit picture Warpzit · Jan 31, 2012 · Viewed 10.1k times · Source

When using a AsyncTaskLoader how would you update a progressbar showing the status as it is being updated? Normally you wait for the callback to remove when done, but how to do running updates? Would you let the main thread (ui) poll the data as it is being set or something?

Edit: I'm talking about AsyncTaskLoader, look at the loader part. Here is link to class: http://developer.android.com/reference/android/content/AsyncTaskLoader.html

I want to use it because its the future :), I know how to do this with AsyncTask.

Answer

garmax1 picture garmax1 · Apr 14, 2012

You can use handler, i think it will be lighter for system than intent

public class MyFragmentActivity extends FragmentActivity{
private final static int MSGCODE_PROGRESS = 1;
private final static int MSGCODE_SIZE = 2;

    private final Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
        Bundle bundle = msg.getData();
        if(null != bundle){
            int data = msg.getData().getInt(BUNDLE_PROGRESS);
            if(msg.what == MSGCODE_SIZE){
                mProgressBar.setMax(data);
            } else if(msg.what == MSGCODE_PROGRESS){
                mProgressBar.setProgress(data);
            }
        }
    }
};
}

Set mHandler to constructor of AsyncTaskLoader and from loadInBackground you can update progress

Bundle bundle = new Bundle();
bundle.putInt(BUNDLE_PROGRESS, lenghtOfFile);
Message msg = new Message();
msg.setData(bundle);
msg.what = MSGCODE_SIZE;
mHandler.sendMessage(msg);