How can we get the Background data usage setting for an application in android? Also can we changes this settings by code?
But here is a list of some of the highest data usage apps you'll likely have on your phone: Streaming apps such as Netflix, Stan and Foxtel Now. Social media apps such as Tik Tok, Tumblr and Instagram. GPS and ridseharing apps such as Uber, DiDi and Maps.
Note: Root access alone may not be enough
Getting/setting background data restriction requires "android.permission.MANAGE_NETWORK_POLICY" permission, which is protection level is "signature". So your app have to be signed with the same key as platform's.
Anyhow, following worked on my end (Android 4.2.2/API level 17, tested with Google Chrome):
/** NetworkPolicyManager.POLICY_NONE */
private static final int POLICY_NONE = 0x0;
/** NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND */
private static final int POLICY_REJECT_METERED_BACKGROUND = 0x1;
/** Context.NETWORK_POLICY_SERVICE */
private static final String NETWORK_POLICY_SERVICE = "netpolicy";
/** NetworkPolicyManager object*/
private Object mPolicyManager;
// lots of code
mPolicyManager = getSystemService(NETWORK_POLICY_SERVICE);
// moar code
private boolean getAppRestrictBackground() {
try {
ApplicationInfo chrome = getPackageManager().getApplicationInfo("com.android.chrome", 0);
try {
Method getUidPolicy = mPolicyManager.getClass().getMethod("getUidPolicy", int.class);
try {
final int uidPolicy = (Integer) getUidPolicy.invoke(mPolicyManager, chrome.uid);
return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
private void setAppRestrictBackground(boolean restrictBackground) {
try {
ApplicationInfo chrome = getPackageManager().getApplicationInfo("com.android.chrome", 0);
try {
Method setUidPolicy = mPolicyManager.getClass().getMethod("setUidPolicy", int.class, int.class);
try {
setUidPolicy.invoke(mPolicyManager, chrome.uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
References:
UPDATE
How can I sign my application with platform key?
You have to have the platform key: there are no tricks here, you just need to have the key. See "Sign your application with your private key" for further details. Normally, OEMs keep their platform keys with the highest possible security. That's, third-party developers would never have access to the platform key. Note that, even if you do get hold of the platform key, you will need to create separate application for each OEM's device(s) as every OEM uses its own unique key.
At the end, unless you are working directly with the OEM, it is not worth the effort.
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