如何在Android中获取任何应用程序后台数据使用状态

时间:2013-07-23 05:51:49

标签: android

我们如何在android中获取应用程序的后台数据使用设置?我们也可以通过代码更改此设置吗?

enter image description here

1 个答案:

答案 0 :(得分:7)

注意 :仅凭root权限可能不够

获取/设置后台数据限制需要"android.permission.MANAGE_NETWORK_POLICY" permission, which is protection level is "signature"。因此,您的应用必须使用与平台相同的密钥进行签名。

无论如何,以下是我的最终结果(Android 4.2.2 / API级别17,使用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();
    }
}

参考文献:

<强>更新

  

如何使用平台密钥签署我的应用程序?

你必须拥有平台密钥:这里没有技巧,你只需要拥有密钥。有关详细信息,请参阅“Sign your application with your private key”。通常,OEM会以最高的安全性保持其平台密钥。也就是说,第三方开发人员永远无法访问平台密钥。请注意,即使您确实掌握了平台密钥,您也需要为每个OEM设备创建单独的应用程序,因为每个OEM都使用自己独特的密钥。

最后,除非您直接与OEM合作,否则不值得努力。