【问题标题】:How to update notification bar downloading a file?如何更新通知栏下载文件?
【发布时间】:2019-05-17 05:35:32
【问题描述】:

谁能解释我做错了什么,我正在下载一个文件,并且我有一个更新的通知进度条。 进度条工作“正常”,因为它得到了更新,但问题是当功能完成下载罚款 我的进度条没有更新完成的消息,我尝试通过这样做来隐藏进度条...

 notification.setProgress(0, 0, false);
notification.setContentTitle("Complete");
notification.setOngoing(false);
notification.setPriority(NotificationCompat.PRIORITY_HIGH);
notificationManager.notify(idd, notification.build());
Log.e(TAG, "1");

我的程序忽略了所有这些,只记录 1

private boolean saveFile(ResponseBody body, String fileName, String id) {

        try {
            // todo change the file location/name according to your needs
            File futureStudioIconFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);

            InputStream inputStream = null;
            OutputStream outputStream = null;


            final int idd = Integer.parseInt(id);

            Intent activityIntent = new Intent(this, MainActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, activityIntent, 0);
            final NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_2_ID)
                    .setSmallIcon(R.drawable.ic_cloud_download_black_24dp)
                    .setContentTitle("Download")
                    .setContentText("Downloading in process")
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setContentIntent(contentIntent);

            notificationManager.notify(idd, notification.build());

            try {
                byte[] fileReader = new byte[4096];
                long fileSize = body.contentLength();
                long fileSizeDownloaded = 0;

                inputStream = body.byteStream();
                outputStream = new FileOutputStream(futureStudioIconFile);

                while (true) {
                    int read = inputStream.read(fileReader);

                    if (read == -1) {

                        break;
                    }

                    outputStream.write(fileReader, 0, read);

                    fileSizeDownloaded += read;

                    Log.e(TAG, "file download: " + fileSizeDownloaded + " of " + fileSize);
                    Long fileSizeD = fileSizeDownloaded;
                    Long fileS     = fileSize;
                    int down = fileSizeD.intValue();
                    int max  = fileS.intValue();
                    Log.e(TAG, "MAX: " + max + " Down: "+ down);
                        notification.setProgress(max, down, false);
                        notificationManager.notify(idd, notification.build());
                }

                outputStream.flush();

                notification.setProgress(0, 0, false);
                notification.setContentTitle("Downloasdfasad");
                notification.setOngoing(false);
                notification.setPriority(NotificationCompat.PRIORITY_HIGH);
                notificationManager.notify(idd, notification.build());
                Log.e(TAG, "1");

                return true;
            } catch (IOException e) {
                return false;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();

                }
            }
        } catch (IOException e) {
            return false;
        }

    }

【问题讨论】:

    标签: android download progress-bar android-progressbar


    【解决方案1】:

    这样试试

    Intent activityIntent = new Intent(this, MainActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, activityIntent, 0);
            final NotificationCompat.Builder notification = new NotificationCompat.Builder(this, CHANNEL_2_ID)
                    .setSmallIcon(R.drawable.ic_cloud_download_black_24dp)
                    .setContentTitle("Download")
                    .setOnlyAlertOnce(true)
                    .setContentText("Downloading in process")
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setContentIntent(contentIntent);
    
    notificationManager.notify(idd, notification.build());
    

    再来一次

    notification.setProgress(0, 0, false);
    notification.setContentTitle("Download Finished");
    notificationManager.notify(idd, notification.build());
    Log.e(TAG, "1");
    return true;
    

    参考:https://stackoverflow.com/a/16435330/5502638

    【讨论】:

      【解决方案2】:

      您可以尝试在 while 循环中执行此操作

      我不确定这是否是正确的方法,但它会解决你的问题

      int max2 = max - 62500;
                          if( max2 > down) {
                              notification.setProgress(max, down, false);
                              notificationManager.notify(idd, notification.build());
                          } else {
                                  notification.setProgress(0, 0, false);
                                  notification.setContentText("Download Complete");
                              notificationManager.notify(idd, notification.build());
      
                          }
      

      【讨论】:

        猜你喜欢
        • 2013-06-25
        • 1970-01-01
        • 1970-01-01
        • 2014-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多