我有一个工作线程的服务。同时在while循环中,通知栏会更新。但并非所有值都得到更新,有时即使显示消息已完成仍未显示最后一个值。
我究竟做错了什么?这是基于Google docs
public class DownloadService extends Service {
NotificationCompat.Builder builder;
NotificationManagerCompat notificationManager;
private int PROGRESS_CURRENT = 0;
public DownloadService() {}
@Override
public void onCreate() {
super.onCreate();
Log.d("onCreate","Created");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(this.isServiceRunning)return Service.START_NOT_STICKY;
Log.d("onStartCommand","Started");
this.isServiceRunning = true;
int PROGRESS_MAX = 100;
notificationManager = NotificationManagerCompat.from(this);
builder = ((App)this.getApplicationContext()).getNotificationBuilder(
"1",
"Download",
"downloading",
"Downloading",
"Hello");
startForeground(1,builder.build());
new Thread(()->{
while(PROGRESS_CURRENT <=100){
notificationManager.notify(1, builder.setProgress(PROGRESS_MAX, PROGRESS_CURRENT, false).build());
PROGRESS_CURRENT++;
}
//Notification bar doesnt get updated?????
notificationManager.notify(1, builder.setContentText("Completed").setProgress(0, 0, false).build());
}).start();
return Service.START_NOT_STICKY;
}
}
您是否尝试在重新发布通知之前取消通知?
notificationManager.cancel(1);
notificationManager.notify(1, builder.setContentText("Completed").setProgress(0, 0, false).build());