androidandroid-workmanagermiui

WorkManager not repeating the PeriodicWorkRequest


I am creating an android application to run my code in background. I'm well aware of the restriction introduced by the Android Oreo for background services and that's why I'm using WorkManager API to schedule the task for the execution. I'm testing my code on Mi Max device with Android API 24 (Nougat) and also enable the auto start manually so that MIUI allows the app to run in background but the problem is, the WorkManager fires for the first time the application starts but after that, it doesn't work. Below is my code I'm using for the periodic work request and work itself.

PeriodicWorkRequest call:

PeriodicWorkRequest work = new PeriodicWorkRequest.Builder(ClassExtendingWorker.class, 15, TimeUnit.MINUTES)
            .setConstraints(Constraints.NONE)
            .build();
WorkManager.getInstance().enqueue(work);

ClassExtendingWorker:

public Result doWork() {
    /*--- SHOWING NOTIFICATION AS AN EXAMPLE TASK TO BE EXECUTED ---*/
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), "")
            .setSmallIcon(R.drawable.common_google_signin_btn_icon_light)
            .setContentTitle("TestApp")
            .setContentText("Code executed")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
    notificationManager.notify(1234, mBuilder.build());

    return Result.SUCCESS;
}

Solution

  • Update your WorkManager version to 1.0.0-alpha04. You can check release notes here

    Also, refer to this PeriodicWorkRequest GitHub demo and update TimeUnit as per your requirement in DayIncrementViewModel.java. It will work as per your need.

    WorkManager is still in alpha mode so it will work completely for all devices once they release the final version.