NetworkType.UNMETERED vs NetworkType.METERED - PeriodicWork

Viewed 3131

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

2 Answers

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.

As of Android 12, the user can choose whether any particular Wi-Fi is metered or not in Settings / Connections / Wi-Fi / network settings / View more. So for example if they have a mobile hotspot on a limited data plan, they can set this one to Metered, but if they have unlimited Wi-Fi at home, they can set this one to Unmetered.

The default of "detect automatically" assigns Unmetered to most Wi-Fi networks, unless the router's DHCP server sets the value ANDROID_METERED in a Vendor Specific Option when assigning the IP address. Android's own "Mobile Hotspot" option does this, so if one Android device connects to the Wi-Fi hotspot of another Android device, it will default to metered, but other Wi-Fi will default to unmetered (and in both cases it can be overridden by the user as described above).

Mobile data networks are always treated as "metered" by Android (at least Android 9+) and I don't think there's any way to override this, other than rooting your device with a customised OS, or using a second device as a hotspot (and overriding its Wi-Fi to Unmetered).

I don't have any special insider knowledge, but I wouldn't be surprised if Google is contractually obliged to some carriers not to put an "unmetered mobile data" option into Android. Carriers that advertise "unlimited" data are rarely truly unlimited; usually it's just a word they use to attract new customers, but the small print gives a "fair use limit" that might be lower than you think. Some UK carriers have said "unlimited but no tethering", meaning the data must be used from the phone itself (and yes they can check the TCP hop count, plus on some phones the SIM card can instruct the phone to disable its tethering options); others have said "tethering allowed but there's a data limit on it"; obviously this is with the expectation that the phone itself will use less data than whatever you connect to its hotspot. Carriers have also been known to disallow app-update traffic in their firewalls, with the expectation that customers will also have access to Wi-Fi that lets them run the heavy updating.

Some apps ask the user whether they want to do something over Wi-Fi only (or unmetered Wi-Fi only), so you could let the user decide.

Related