Krishnakant Dalal
Krishnakant Dalal

Reputation: 3578

How to get Any Application Background data Usage Status in 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

Upvotes: 8

Views: 2938

Answers (1)

ozbek
ozbek

Reputation: 21183

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.

Upvotes: 7

Related Questions