20

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;
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Madhusudan Sharma
  • 403
  • 1
  • 3
  • 12

5 Answers5

23

according to the documentation:

 /**
 * The minimum interval duration for {@link PeriodicWorkRequest} (in milliseconds).
 */
public static final long MIN_PERIODIC_INTERVAL_MILLIS = 15 * 60 * 1000L; // 15 minutes.
/**
 * The minimum flex duration for {@link PeriodicWorkRequest} (in milliseconds).
 */
public static final long MIN_PERIODIC_FLEX_MILLIS = 5 * 60 * 1000L; // 5 minutes.

if you are setting this two value less than min specified, then you have to wait approx 20 min to see the log/output

Abhilash Das
  • 1,388
  • 1
  • 16
  • 22
12

If you use PeriodicWorkRequest with interval less than 15 min, you should return Result.retry(), not success or failure.

Sylvester Yao
  • 237
  • 2
  • 7
  • 2
    It's incorrect! This will retry linearly e.g. first after 1 min, second after 3 min, third after 6 min... So there is no desired clear spacing – Vlad Feb 14 '20 at 10:13
5

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.

Viraj Patel
  • 2,113
  • 16
  • 23
  • 1
    Does this work for Oreo? I tested this application. It works fine till Nougat but not on Oreo. Btw, I have changed the PeriodicWorkRequest to 10 minutes. – shakunthalaMK Aug 20 '18 at 08:01
  • 2
    @shakunthalaMK The default time for PeriodicWorkRequest is 15 minutes. So below 15 minutes, it will not work properly. Moreover, it will not fire exactly on 15 minutes but it will execute approximately at 15 minutes. If you want to execute any task every 10 minutes or on any specific period, you have to use Alarm Manager instead. – Viraj Patel Aug 20 '18 at 08:05
  • 1
    @shakunthalaMK WorkManger latest version is `1.0.0-alpha07` so use latest one because some problem related PeriodicWorkRequest has been resolved after `1.0.0-alpha04`. – Viraj Patel Aug 20 '18 at 08:07
  • 1
    Hey it works fine on Oreo. As you said it took more than 15mins in one of the case to trigger. One last doubt.. Will it work when the app is killed as well? Thanks – shakunthalaMK Aug 20 '18 at 09:12
  • Yes, it will work as well when App is killed or in background. Basically, it is a wrapper class of Service, JobScheduler, FirebaseJobDispatcher and AlarmManger. Furthermore, it will work without PlayService. [Check this Codelabs link](https://codelabs.developers.google.com/codelabs/android-workmanager/#0) – Viraj Patel Aug 20 '18 at 09:19
  • Ok Thanks for the help :) – shakunthalaMK Aug 20 '18 at 10:31
  • Please note that from the google docs: `Periodic work has a minimum interval of 15 minutes and it cannot have an initial delay. ` See: https://developer.android.com/reference/androidx/work/PeriodicWorkRequest – Tanasis Mar 23 '19 at 17:41
  • same issue with android.arch.work:work-runtime:1.0.1 – Shubham AgaRwal Apr 21 '19 at 15:57
  • Hi, @VirajPatel after reboot, it is not working. What should I do to make it work after reboot ? I am trying it on Android 9. – Darth Vader May 03 '20 at 09:55
  • Same here. I start the app, PeriodicWorkRequest works perfectly. When I the. Reboot the device and unlock it, the same PeriodicWorkRequest does never fire up. – thomasgalliker Dec 23 '22 at 18:58
2

Add flex interval and flex unit to builder constructor. In my case that's started to work.

PeriodicWorkRequest work = new PeriodicWorkRequest.Builder(ClassExtendingWorker.class, 15, TimeUnit.MINUTES, 5, TimeUnit.MINUTES)
karmil32
  • 133
  • 1
  • 6
  • Hi I am having a problem where my periodic requests only work when I use a flex period. Do you have an explanation for this ? Or what do you differently now ? – rial Aug 12 '20 at 16:31
0

I have solved the problem by changing these codes in the Work Manager.

  1. Use try & catch block in the Worker class to get the Result.success() and Result.failure()
  2. Change the ExistingPeriodicWorkPolicy from KEEP to UPDATE.