如何计算Progress Bar的下载进度

时间:2011-08-22 12:19:29

标签: android download progress-bar

来自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 :(得分:0)

解决方案是简单地以不会的方式编写代码 超载通知引擎。