Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WorkManager's PeriodicWorkRequest is executing once and not repeating

I am trying to make use of WorkManager however I seem to be running into an issue where PeriodicSync is only repeated on startup once and that's it.

I've looked at this post Is WorkManager's PeriodicWorkRequest really repeating for anyone? but there is no answer there. I am using the latest alpha build 10 as well.

If someone could help, it would be greatly appreciated. I am still new to android but need to get this to work for a project. I haven't even tried using it with the code I want but simply trying to get it to run correctly.

I set it to run every 10 seconds, I also tried 10,000 ms but neither worked and nothing happened after 10 seconds.

The Log message "Sync" only appears once onCreate and that's it.

Another issue I have is that every time I launch my app, it seems like the number of workers increases, as if they are getting added on top of each other and I don't know if that's related. Answer to my other issue was found here but I still need help with my primary issue.

Here's my code:

The Worker Class

public class MyWorker2 extends Worker {

    private static final String TAG = "BOOGABOOGA";

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

    @Override
    public Worker.Result doWork() {

        // Do the work here--in this case, compress the stored images.
        // In this example no parameters are passed; the task is
        // assumed to be "compress the whole library."
        Log.i(TAG, "Sync");

        // Indicate success or failure with your return value:
        return Result.SUCCESS;

    }
}

MainActivity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        PeriodicWorkRequest syncWork = new PeriodicWorkRequest.Builder(MyWorker2.class, 10, TimeUnit.SECONDS).build();
        WorkManager.getInstance().enqueue(syncWork);

    }
}

Edit: Reason this isn't working is because minimum interval to repeat a task is set to 15 minutes per the spec found here: https://developer.android.com/reference/androidx/work/PeriodicWorkRequest

like image 211
GevDev Avatar asked Oct 21 '18 13:10

GevDev


People also ask

What is the difference between onetimeworkrequest and periodicworkrequest?

WorkManager has support for both asynchronous one-off and periodic tasks. For a one-off task, a OneTimeWorkRequest is used, that is, a WorkRequest that will only execute once. Alternatively, for recurring or tasks that should be run periodically, a PeriodicWorkRequest is used to execute such tasks.

Does periodicworkrequest work in lg-g2?

Except API 23, it works fine. I have tested in LG-G2 (Model LG-D800, Android 5.0.2, API 21) as well as different emulators it is working fine. patelviraj changed the title PeriodicWorkRequest.Builder: Flex duration is currently not supported before API 24. Ignoring. PeriodicWorkRequest not working properly in API 23 on Jun 20, 2018

Why does workmanager create new workrequest every time I work?

This is especially relevant for periodic work, that never reaches a final state, if there are no cancellations. We like to say that WorkManager guarantees the execution of your work even if your application is closed or the device is restarted. So, enqueueing your worker at each start of your application cause to add a new WorkRequest each time.

Why can’t workmanager execute at exact intervals in workmanager?

This has the effect that running a Work can be delayed to the next maintenance window if your device is in Doze mode. As we saw, WorkManager is not about executing at exact intervals. If that is your requirement, you’re looking at the wrong API.


1 Answers

Reason this wasn't working was because the minimum time interval where a task can be repeated is set to 15 minutes. Credit goes to exshinigami. The spec for this can be found here: https://developer.android.com/reference/androidx/work/PeriodicWorkRequest#min_periodic_interval_millis

I just tested and verified that it did indeed repeat after 15 minutes.

like image 94
GevDev Avatar answered Sep 21 '22 12:09

GevDev