Not able to publish progress from async task do in background's while loop - Android

前端 未结 2 1747
自闭症患者
自闭症患者 2021-01-24 00:27

I want to update dialog\'s download progress from doInBackground.
I am printing log as well as publishing progress.
Neither of them are working.

It up

相关标签:
2条回答
  • 2021-01-24 00:59

    One possible explanation for the behaviour is that reading from remote input stream and writing it into output buffer is extremely fast. I tried the same code but with just a loop running for 10 times.

    int total = 0;
    while(total <= 100){
        progress = total;
        total += 10;
        publishProgress();
    }
    

    Even I could not see the progress dialog at first, so put a Thread.sleep() in the loop and the progress dialog works just fine.

    int total = 0;
            while(total <= 100){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                progress = total;
                total += 10;
                publishProgress();
            }
    

    Try to log the time (System.currentTimeMillis()) at which you are writing into the output buffer.

    Hope it helps.

    0 讨论(0)
  • 2021-01-24 01:08

    remove super from onProgressUpdate and then try

    @Override
    protected void onProgressUpdate(Void... values) {
        //super.onProgressUpdate(values);
        mProgressDialog.setProgress(progress);
    }
    

    if it doesn't work than add a sleep statement in your loop so that this loop will free the processor and give time to publish the progress

    try {
        Thread.sleep(200);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    
    0 讨论(0)
提交回复
热议问题