Панель уведомлений Android не обновляет все значения - PullRequest
1 голос
/ 12 марта 2019

У меня есть служба с рабочим потоком. Между тем в цикле while панель уведомлений обновляется. Но не все значения обновляются, и иногда даже сообщение «Завершено» не отображается, оставаясь застрявшим с последним значением.

Что я делаю не так? Это основано на Google документах

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

}

1 Ответ

0 голосов
/ 13 марта 2019

Вы пытались отменить уведомление перед повторной отправкой?

notificationManager.cancel(1);
notificationManager.notify(1, builder.setContentText("Completed").setProgress(0, 0, false).build());
...