Android - 如何在通知栏显示图片上传进度?

Android - How to display Image uploading progress in Notification Bar?

我试图在通知栏中显示上传进度,但当我尝试时,通知变得像 Upload in Progress - Upload Finished 并且不断重复。所以,这是我试过的代码。

我的通知码

private void uploadMediaNotification(int progress) {

NotificationCompat.Builder notification = new NotificationCompat.Builder(this, OfflineCapabilities.CHANNEL_2_ID)
        .setSmallIcon(R.drawable.ic_baseline_cloud_upload_24)
        .setContentTitle("Uploading Media")
        .setContentText("Uploading in Progress")
        .setProgress(100, 0, false)
        .setOngoing(true)
        .setOnlyAlertOnce(true)
        .setPriority(NotificationCompat.PRIORITY_LOW);

compat.notify(2, notification.build());

new Thread(new Runnable() {
    @Override
    public void run() {
        SystemClock.sleep(1000);
        for (int progres = 0; progres < progress; progres += progress) {
            notification.setProgress(100, progres, false);
            compat.notify(2, notification.build());
            SystemClock.sleep(1000);
        }
        notification.setContentText("Uploading Finished")
                .setProgress(0, 0, false)
                .setOngoing(false);
        compat.notify(2, notification.build());
    }
}).start();
}

以及上传任务的代码

final UploadTask uploadTask = reference.putFile(imageURI);
    uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onProgress(@NonNull UploadTask.TaskSnapshot taskSnapshot) {
            double progress = (100 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
            uploadMediaNotification((int) progress);
        }
    });

有什么帮助或建议吗?

你有两个问题。一是函数 uploadMediaNotification 总是 在调用 时使用 notification.builder 创建新通知。为了缓解这种情况,将初始化和更新放在两个不同的函数中,例如 createUploadMediaNotification() 和 updateUploadMediaNotification()

其次,您的 Thread 非常无用,因为您已经获得了传递给函数的进度百分比。这意味着您唯一需要做的就是设置进度,如果进度为 100%(即已完成),则更改文本以说明已完成。

这是一个简单的例子:

private NotificationCompat.Builder notification;

private void createUploadMediaNotification() {
    notification = new NotificationCompat.Builder(this, OfflineCapabilities.CHANNEL_2_ID)
        .setSmallIcon(R.drawable.ic_baseline_cloud_upload_24)
        .setContentTitle("Uploading Media")
        .setContentText("Uploading in Progress")
        .setProgress(100, 0, false)
        .setOngoing(true)
        .setOnlyAlertOnce(true)
        .setPriority(NotificationCompat.PRIORITY_LOW);

    compat.notify(2, notification.build());
}

private void updateUploadMediaNotification(int progress) {

    if(progress != 100) { //Progress is lower than 100%, updating progress
        notification.setProgress(100, progress, false);
    } else { //progress is 100%, changing notification content
        notification.setContentText("Uploading Finished")
                .setProgress(0, 0, false)
                .setOngoing(false);
    }

    //Update the notification
    compat.notify(2, notification.build());
}

有关详细信息,请查看 this example on Android developers。希望它有所帮助。 (代码中有错误请见谅,我只是在浏览器中写的)