Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Any Application Background data Usage Status in Android

Tags:

android

How can we get the Background data usage setting for an application in android? Also can we changes this settings by code?

enter image description here

like image 922
Krishnakant Dalal Avatar asked Jul 23 '13 05:07

Krishnakant Dalal


People also ask

Which apps consume more data in background?

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.


1 Answers

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:

  • DataUsageSummary.java
  • NetworkPolicyManager.java
  • Context.java

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.

like image 189
ozbek Avatar answered Oct 18 '22 02:10

ozbek