具有进度的两个或多个前景通知在更新进度时互相替换 [英] Two or more Foreground Notification with Progress is replacing each other while updating its progress

查看:62
本文介绍了具有进度的两个或多个前景通知在更新进度时互相替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项服务,该服务将在前台运行上传任务,然后在通知中显示进度.由于用户可能使用不同的id请求多次上传,因此将运行两个或多个前台服务.一切正常,但是我想用此代码显示进度的所有任务通知.

I have a service that will run a upload task in foreground then showing a progress in the notification. Since a user may upload multiple times with different id request then there will be two or more foreground service that will run. Everything works fine but what I want is to show all task's notification with progress with this code.

 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID_DEFAULT)
            .setSmallIcon(R.drawable.ic_file_upload_white_24dp)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(caption)
            .setProgress(100, percentComplete, false)
            .setContentInfo(String.valueOf(percentComplete +"%"))
            .setOngoing(true)
            .setAutoCancel(false);

    NotificationManager manager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    manager.notify(code, builder.build());

    startForeground(code,builder.build());

但是不幸的是,通知只是被覆盖,但是如果我删除了startForeground(),那么一切都会正常进行,因为它根据正在进行的任务数量显示了多个进度通知,但是如果内存不足,系统可能会终止该进程,这就是为什么我希望它在前台运行.那么如何显示与前台中正在进行的任务数量相等的通知数量呢?

But unfortunately the notification just get overwrites, but if I removed the startForeground() then everything works fine as it shows multiple progress notification according to how many task is ongoing but the system may kill the process if gets low memory that is why I wanted it to run in foreground. So how am I able to show a number of notification that is equal to the number of on going task in Foreground?

推荐答案

最后,我弄清楚了如何做到这一点. 首先,我需要稍稍延迟地在onCreate内运行它.

At last I figure it out how to do it. First I needed to run it inside a onCreate with a little delay.

 @Override
public void onCreate (){
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
                startForeground(requestCode, getMyActivityNotification("",completedParts,totalSize));
        }
    },500);

}

然后创建一个通知提供程序方法.

Then create a notification provider method.

//Notification provider
private Notification getMyActivityNotification(String caption, long completedUnits, long totalUnits){

    int percentComplete = 0;
    if (totalUnits > 0) {
        percentComplete = (int) (100 * completedUnits / totalUnits);
    }

    //Return the latest progress of task
    return new NotificationCompat.Builder(this, CHANNEL_ID_DEFAULT)
            .setSmallIcon(R.drawable.ic_file_upload_white_24dp)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(caption)
            .setProgress(100, percentComplete, false)
            .setContentInfo(String.valueOf(percentComplete +"%"))
            .setOngoing(true)
            .setAutoCancel(false)
            .build();

}

然后将更新通知进度与调用前台分开.

Then updating notification progress should be separated from calling a foreground.

/**
 * Show notification with a progress bar.
 * Updating the progress happens here
 */
protected void showProgressNotification(String caption, long completedUnits, long totalUnits, int code) {

    createDefaultChannel();
    mCaption = caption;
    requestCode = code;
    completedParts = completedUnits;
    totalSize = totalUnits;

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (mNotificationManager != null) {
        //Update the notification bar progress
        mNotificationManager.notify(requestCode,  getMyActivityNotification(caption,completedUnits,totalUnits));
    }
}

这篇关于具有进度的两个或多个前景通知在更新进度时互相替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆