Как рассчитать прогресс загрузки для ProgressBar - PullRequest
0 голосов
/ 22 августа 2011

из IntentService я открываю Notification, показывая ProgressBar.
Проблема в том, что область уведомлений зависнет, если я не переадресовываю вызовы на 10.
То есть вызов ProgressBar 10 раз 10%, 20%, 30 ...

Ожидается ли такое поведение?
Я хотел бы знать лучший способ сделать это.

Что я делаю сейчас, так это какой-то модный подсчет для начинающих:

long filesize = bis.available();
double Mbyte = (double)filesize/ (1024*1024);
filesize = (filesize - resumeLong);

totalMbSent = totalMbSent +  (double)filesize;
DecimalFormat dec = new DecimalFormat("0.00");
String result = dec.format(Mbyte);

int c10 = 0;int c20 = 0;int c30 = 0;int c40 = 0;int c50 = 0;int c60 = 0;int c70 = 0;int c80 = 0;int c90 = 0;
int proc = (int) (filesize/10);
int proc10 = proc; // 10%
int proc20 = proc+proc; // 20&
int proc30 = proc+proc+proc;  //30%
int proc40 = proc+proc+proc+proc;
int proc50 = proc+proc+proc+proc+proc;
int proc60 = proc+proc+proc+proc+proc+proc;
int proc70 = proc+proc+proc+proc+proc+proc+proc;
int proc80 = proc+proc+proc+proc+proc+proc+proc+proc;
int proc90 = proc+proc+proc+proc+proc+proc+proc+proc+proc;
while ((val = bis.read(buffer, 0, 1024)) > 0) {
    out.write(buffer, 0, val);
    filesize -= val;
    if (filesize < 1024) {
        val = (int) filesize;
    }

    // This is for the notification showing progress in percent
    if ( c10 == 0 && filesize > proc10 && filesize < proc20 ){
        c10 = 1 ;setNotificationProgress(sendingFile+" " + count +
        "/" + max + "  " + result + "Mb", Notification.FLAG_NO_CLEAR,90,false);
    }else if(c20 == 0 && filesize > proc20 && filesize < proc30 ){
        c20 = 1 ;setNotificationProgress(sendingFile+" " + count +
        "/" + max + "  " + result + "Mb" , Notification.FLAG_NO_CLEAR,80,false);
    }else if(c30 == 0 && filesize > proc30 && filesize < proc40 ){
        c30 = 1 ;setNotificationProgress(sendingFile+" " + count +
        "/" + max + "  " + result + "Mb" , Notification.FLAG_NO_CLEAR,70,false);
    }else if(c40 == 0 && filesize > proc40 && filesize < proc50){
        c40 = 1 ;setNotificationProgress(sendingFile+" " + count +
        "/" + max + "  " + result + "Mb" , Notification.FLAG_NO_CLEAR,60,false);
    }else if(c50 == 0 && filesize > proc50 && filesize < proc60){
        c50 = 1 ;setNotificationProgress(sendingFile+" " + count +
        "/" + max + "  " + result + "Mb" , Notification.FLAG_NO_CLEAR,50,false);
    }else if(c60 == 0 && filesize > proc60 && filesize < proc70){
        c60 = 1 ;setNotificationProgress(sendingFile+" " + count +
        "/" + max + "  " + result + "Mb" , Notification.FLAG_NO_CLEAR,40,false);
    }else if(c70 == 0 && filesize > proc70 && filesize < proc80){
        c70 = 1 ;setNotificationProgress(sendingFile+" " + count +
        "/" + max + "  " + result + "Mb" , Notification.FLAG_NO_CLEAR,30,false);
    }else if(c80 == 0 && filesize > proc80 && filesize < proc90){
        c80 = 1 ;setNotificationProgress(sendingFile+" " + count +
        "/" + max + "  " + result + "Mb" , Notification.FLAG_NO_CLEAR,20,false);
    }else if(c90 == 0 && filesize > proc90 && filesize < filesize){
        c90 = 1 ;setNotificationProgress(sendingFile+" " + count +
        "/" + max + "  " + result + "Mb" , Notification.FLAG_NO_CLEAR,10,false);
    }
}

1 Ответ

0 голосов
/ 05 июля 2012

Решение состоит в том, чтобы просто написать код таким образом, чтобы он не
перегружал механизм уведомлений.

...