How to get programmatically the data usage limit set by user on Android OS configuration?
J

2

21

User can define at Data Usage screen a limite and/or a warning limit for mobile data usage. So how can I get this information by code?

Screen of Data Usage configuration of native OS.

Data usage OS system configuration screen

I wanna the limit value and warning value.

I've already tried this but not work and always return NULL to both:

final Long recommendedBytes = DownloadManager.getRecommendedMaxBytesOverMobile( this.context );
final Long maximumBytes = DownloadManager.getMaxBytesOverMobile( this.context );

// recommendedBytes and maximumBytes are NULL

And TrafficStats class just have a data transferred not the limits.

Juster answered 23/6, 2014 at 12:31 Comment(3)
Did you set the INTERNET permissions?Turnsole
@Manu Yes and more... I tryed with all this: INTERNET, ACCESS_NETWORK_STATE, ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION, CHANGE_WIFI_STATE, ACCESS_WIFI_STATE, READ_SYNC_SETTINGS. Some no make sense but I tried all I thought.Juster
@xpto did you get any workaround solution? Me also facing the same problem, but for me NetworkPolicyManager is made hidden is an issue and I can't find any public API for same.Laktasic
J
19

After days searching and research about this problem I couldn't find a answer for that. Bellow I will lift every attempt that I did.

1. Download Manager

With this class you can start download over any network or device state and it will handle all states e.g. network loss, device reboot, etc...

There are two methods called getMaxBytesOverMobile and getRecommendedMaxBytesOverMobile, they was a pretty candidate to solve this problem at first time. But after code tests and Download Manager implementantion research I'd found that there is no way to get thoose values by DownloadManager.

Reason

Thoose methods call Settings.Secure.getLong with they respective labels Settings.Secure.DOWNLOAD_MAX_BYTES_OVER_MOBILE and Settings.Secure.DOWNLOAD_RECOMMENDED_MAX_BYTES_OVER_MOBILE in the turn makes a call to a lazy String map inside inside a inner class called NameValueCache.

Ok so far but none of inner classes or Settings implementation it self use DOWNLOAD_MAX_BYTES_OVER_MOBILE or DOWNLOAD_RECOMMENDED_MAX_BYTES_OVER_MOBILE inside.

I considered the lazy map was populate by a third entity, what actually happens, so I found the NameValueTable Settings inner class that handle the new values to lazy map. The putString is a protected method call by Settings.Secure and Settings.System inner classes (calls of Secure and System).

So I could conclude that if the OS implementantion do not put thoose String values I can't get them.

2. TrafficStats

Just a quick look on official reference I could notice that it will not help me because this class just provide the amount of bytes and packages that was trafficked since last device boot.

http://developer.android.com/reference/android/net/TrafficStats.html

3. NetworkPolicyManager and NetworkPolicy

As @bina posted here the both classes are hidden and could not be use by normal apps e.g. that will be published in Google Play.

https://mcmap.net/q/603167/-how-to-get-programmatically-the-data-usage-limit-set-by-user-on-android-os-configuration

4. ConnectivityManager

In short, you just can get the NetworkInfo that not provide much information about user preferences (really none!). Just provide informations about network and e.g. mobile network provider.

http://developer.android.com/reference/android/net/ConnectivityManager.html

After all I assume that no way to get this information nowadays. Please if you read it and found a way post here!

Thanks for all.

PS.: Sorry by english mistakes.

Juster answered 2/7, 2014 at 15:27 Comment(0)
B
11

Do you want to get limit value(5GB) and warnning value(2GB) in this example?

If so, you can get limitBytes and warningBytes by the following code, if you can use android.permission.MANAGE_NETWORK_POLICY and android.permission.READ_PHONE_STATE.
However, android.permission.MANAGE_NETWORK_POLICY protectionLevel is signature.

NetworkPolicyManager manager = (NetworkPolicyManager) getSystemService("netpolicy");
NetworkPolicy[] networkPolicies = manager.getNetworkPolicies();
Log.d("NetworkPolicy", "limitBytes is " + networkPolicies[0].limitBytes);
Log.d("NetworkPolicy", "warningBytes is " + networkPolicies[0].warningBytes);

(NetworkPolicyManager and NetworkPolicy classes are hidden)

Betts answered 27/6, 2014 at 6:36 Comment(3)
Thanks @bina, I've tried this too but I need this value to general apps not especially kinds. With reflection I can invoke the NetworkPolicyManager easily but I need setup the permission that you notice, so it's became a problem again.Juster
It is so painful that NetworkPolicyManager is a hidden class :(Frymire
This code is now deprecated in android 11 as it's a Non-Sdk API and Android marked it as a hidden apiLaktasic

© 2022 - 2024 — McMap. All rights reserved.