Answer
    • This is an ongoing notification . NotificationCompat.Builder has method #setProgress which you can use .
      • 2
    • I don't think so . Well I do not have much experience with work manager so I can not make any specific comments on this .

Here is something which i have in my mind that can help. The idea is to create a worker and put your logic to get the progress and show updates as a notification using handler.

NOTE : This code is not tested and i am using it just to explain the way

Worker Code

public class CompressWorker extends Worker {

public CompressWorker(@NonNull Context context, @NonNull WorkerParameters params) {
    super(context, params);
}

@NonNull
@Override
public Result doWork() {
   ProgressManager manager = new ProgressManager();
   int i = 0;
   while(i<100){
       i++;
       try {
           Thread.sleep(1000);
           manager.updateProgress(i);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
   }
    Log.i("Ansh", "worker's job is finished");
    // Indicate success or failure with your return value:
    return Result.success();

    // (Returning Result.retry() tells WorkManager to try this task again
    // later; Result.failure() says not to try again.)
}}

And there is another class using the handler to send the updates to the notification

public class ProgressManager {

Context context;
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);

Handler handler = new Handler(Looper.getMainLooper()) {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
                context);
        notificationBuilder.setProgress(100, msg.arg1, true);
        Notification notification = notificationBuilder.build();
        notificationManagerCompat.notify(1000, notification);
    }
};


public void updateProgress(int val) {
    Message msg = new Message();
    msg.arg1 = val;
    handler.sendMessageDelayed(msg, 1000);
}}
  • 1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions