1

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;
    }

}
  • This may not be the reason of your problem but initialization of `PROGRESS_MAX` and `notificationManager` should not be in the loop. Move those up and remove the second `notificationManager` init. – underoid Mar 12 '19 at 21:33
  • @underoid Same thing happens –  Mar 12 '19 at 21:59

1 Answers1

0

Have you tried to cancel the notification before reposting it?

notificationManager.cancel(1);
notificationManager.notify(1, builder.setContentText("Completed").setProgress(0, 0, false).build());
Bruno Martins
  • 1,347
  • 2
  • 11
  • 32