0

In my application I am using Work manager for Periodic Work. In below code if device has internet connection my work is executing after every 4 hr.

PeriodicWorkRequest.Builder myWorkBuilder =
                new PeriodicWorkRequest.Builder(FileUpload.class, 4,
                        TimeUnit.HOURS)
                        .setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build());

        PeriodicWorkRequest myWork = myWorkBuilder.build();
        WorkManager.getInstance().enqueueUniquePeriodicWork("myJob", ExistingPeriodicWorkPolicy.REPLACE,myWork);

Now I want to change time - If device is connected to WIFI then work should execute after every 20 min and if device is connected to cellular data then work should execute after 12 hrs. How to achieve this. Thanks in advance

karthik
  • 528
  • 4
  • 19
PPD
  • 5,660
  • 12
  • 52
  • 86

1 Answers1

0

One possible solution is to add constraints of NetworkType.CONNECTED in your PeriodicWorkRequest. And After that in your FileUpload.class check whether you are connected with WIFI or cellular data by using this and then act accordingly

Muhammad Muzammil
  • 1,313
  • 1
  • 12
  • 28
  • How to get constraints in FileUpload class? – PPD Aug 06 '18 at 13:35
  • You have already set the required constraint `(NetworkType.CONNECTED)`. Now By using [this](https://stackoverflow.com/questions/17148371/check-whether-mobile-is-using-wifi-or-data-3g), you can execute your work accordingly – Muhammad Muzammil Aug 07 '18 at 05:19
  • Hi, In FileUpload class I can check whether its connected to WIFI or cellular data, But my decision to call upload function (Inside FileUpload class) is already written in other class in which I had created PeriodicWorkRequest. So how I will decide in FileUpload class when to call function upload ()? – PPD Aug 09 '18 at 13:14