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.
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.
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.
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,
There are two methods called
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.
Thoose methods call
the turn makes a call to a lazy
Stringmap inside inside a
inner class called
Ok so far but none of inner classes or
I considered the lazy map was populate by a third entity, what
actually happens, so I found the
inner class that handle the new values to lazy map. The
protectedmethod call by
Settings.Systeminner classes (calls of Secure and
So I could conclude that if the OS implementantion do not put thoose String values I can’t get them.
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.
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.
In short, you just can get the
NetworkInfothat not provide
much information about user preferences (really none!). Just provide
informations about network and e.g. mobile network provider.
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.
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.limitBytes); Log.d("NetworkPolicy", "warningBytes is " + networkPolicies.warningBytes);
(NetworkPolicyManager and NetworkPolicy classes are hidden)