如何计算ProgressBar的下载进度

发布于 2024-11-30 13:15:11 字数 3394 浏览 2 评论 0原文

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

from an IntentService i open a Notification showing a ProgressBar.
The problem is that the notification area will freeze if i dont dived the calls by 10.
That is calling the ProgressBar 10 times 10%, 20%, 30 ...

Is this behavior expected?
I would like to know a better way of doing this.

What i do now is some fancy beginner calculation like this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪你到最终 2024-12-07 13:15:11

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

The solution is to simply write the code in a way that it does not
overload the notification engine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文