0

Workmanager is not launching the worker. I have a debug breakpoint in the worker class. I am not hitting breakpoint. I am following the example provided by WorkManager tutorial. I am not sure where I am going wrong.

I have created following worker class

public class MessageSyncWorker extends Worker {


    public MessageSyncWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        Log.d(MessageSyncWorker.class.getSimpleName(), "In Message Sync Worker");
        return Result.success();
    }
}

I am enqueuing the work in MainActivity as below

    private void CreateWorkRequest() {
        Data.Builder dataBuilder = new Data.Builder();
        dataBuilder.putString("URI", "http//192.168.1.103:5168/api/sync");
        Constraints constraints = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED).build();
        PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(MessageSyncWorker.class, 1, TimeUnit.MINUTES, 3, TimeUnit.MINUTES)
                .addTag("MessageSyncWorker")
                .setInputData(dataBuilder.build()).build();

        WorkManager workManager = WorkManager.getInstance(getApplicationContext());
        workManager.enqueue(workRequest);

    }
fossil
  • 740
  • 6
  • 17
  • You can't have less than 15 minutes as work interval. https://developer.android.com/reference/androidx/work/PeriodicWorkRequest#MIN_PERIODIC_INTERVAL_MILLIS – Zain Jan 18 '22 at 04:48
  • I suppose I can increase the interval for more than 15 minutes. But, any suggestions if I need intervals less than 15 minutes? – fossil Jan 18 '22 at 13:28
  • Unfortunately you can't with WorkManager, you can do it with `AlarmManager`. [check this](https://stackoverflow.com/questions/26573341/scheduling-alarm-every-2-minutes-android) – Zain Jan 18 '22 at 13:52

1 Answers1

0

First thing make sure your PeriodicWorkRequest is not created multiple times and you can check with WorkManager.enqueueUniquePeriodicWork method.

You can find more information on WorkManager documentation. The basic is to use WorkManager.enqueueUniquePeriodicWork(String, ExistingPeriodicWorkPolicy, PeriodicWorkRequest) instead of the WorkManager.enqueue(PeriodicWorkRequest) you’re currently using.

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

Enes
  • 110
  • 1
  • 1
  • 9