我有一个带有工作线程的服务。同时,在while循环中,通知栏得到更新。但是并非所有值都会更新,有时甚至未显示消息Completed仍然停留在最后一个值上。
我在做什么错?这是基于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;
}
}