Android- download file + status bar notification slowing down phone

后端 未结 4 1576
难免孤独
难免孤独 2021-02-10 06:22

I currently have an asynctask which downloads a mp3 from a server. When the user starts to download it, a status bar notification is created. This displays the prog

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-10 07:15

    I too experienced this problem. I was updating the progress bar WAY too often (even when the progress didn't change), here's how I fixed that:

            // While loop from generic download method.
            int previousProgress = 0;
            while ((count = inputStream.read(buff)) != -1) {
                outputStream.write(buff, 0, count);
                totalBytesDownloaded += count;
                int prog = (int) (totalBytesDownloaded * 100 / contentLength);
                if (prog > previousProgress) {
                    // Only post progress event if we've made progress.
                    previousProgress = prog;
                    myPostProgressMethod(prog);
    
                }
            }
    

    Now the app runs great and the user still receives a progress notification.

提交回复
热议问题