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

前端 未结 2 1746
自闭症患者
自闭症患者 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.

提交回复
热议问题