in my application I am using work manager for periodic work. I am uploading files to server. I have one button on click of that button one dialog shown up and ask user - Which network you want to use while uploading file - 1. Wifi 2. Any
If user click on wifi I am uploading file after every 30 Min, If user click on Any I am uploading file after every 1 hr.
Following is my code for this: 1. If user select WIFI
PeriodicWorkRequest.Builder wifiWorkBuilder =
new PeriodicWorkRequest.Builder(FileUpload.class, 30,
TimeUnit.MINUTES)
.addTag("WIFIJOB1")
.setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.UNMETERED).build());
wifiWork = wifiWorkBuilder.build();
WorkManager.getInstance().enqueueUniquePeriodicWork("wifiJob", ExistingPeriodicWorkPolicy.REPLACE, wifiWork);
If User select Any:
PeriodicWorkRequest.Builder mobileDataWorkBuilder =
new PeriodicWorkRequest.Builder(FileUpload.class, 1,
TimeUnit.HOURS)
.addTag("MOBILEDATAJOB1")
.setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build());
mobileDataWork = mobileDataWorkBuilder.build();
WorkManager.getInstance().enqueueUniquePeriodicWork("mobileDataJob", ExistingPeriodicWorkPolicy.REPLACE, mobileDataWork);
For any network it works perfectly and upload apk after every 1 hr. But if user select Wifi then here is problem -
If user connected to wifi of other mobile(say he is using hotspot) so here network is I guess consider as Metered network so it will not upload file. I just want to know our House or office network are by default are Unmetered network or not. If suppose its not fix (Means some are metered and some are unmetered) then using this code if user select wifi and user wifi is considered as metered then from his device file will never get uploaded.
Or should I create another task like :
PeriodicWorkRequest.Builder meteredwifiWorkBuilder =
new PeriodicWorkRequest.Builder(FileUpload.class, 45,
TimeUnit.MINUTES)
.addTag("METEREDWIFIJOB")
.setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.METERED).build());
wifiWork = wifiWorkBuilder.build();
WorkManager.getInstance().enqueueUniquePeriodicWork("meteredwifiJob", ExistingPeriodicWorkPolicy.REPLACE, wifiWork);
So if user not connected to wifi file will be uploaded after every 1 hr, If connected to wifi (unmetered) file will be uploaded after every 30 min and if connected to metered wifi then file will be uploaded after every 45 min.
Is above logic make sense to create 3 sepearte task to upload file. Any suggestion will be appreciated. Thanks in advance
In technical terms, Wi-Fi access points or connections with unlimited data access are termed unmetered connections, while those that come with data caps are termed metred connections. Smartphones or Android devices automatically restrict heavy data usage like online backup, updates download etc.
Google made the process of marking a network as metered vastly easier in Android P. All you need to do is open your WiFi settings, tap on a network, and find the "Metered" option under "Advanced." Open that, and you can set the network as default network preference, metered, or unmetered.
If all you care about is a presence of a network connection just use NetworkType.CONNECTED
. If the file is very big, and could cost the user (as they will end up using an expensive data connection) you should use NetworkType.UNMETERED
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With