Update progressbar from AsyncTaskLoader?

前端 未结 6 1695
粉色の甜心
粉色の甜心 2021-01-31 05:30

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

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-31 06:15

    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);
    

提交回复
热议问题