I have a service with a worker thread. Meanwhile in the while loop, the notification bar gets updated. But not all values get updated and sometimes even the message Completed is not shown remaining stuck with the last value.
What am i doing wrong? This is based from 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;
}
}