更改Android设备管理员设置

时间:2013-08-01 15:54:50

标签: android root android-source

如果你进入你的Android设备设置 - >安全性,在“设备管理”部分下有一些验证应用程序的设置。

在AOSP工作期间,我正在尝试使用一些测试软件。部分原因是我需要启用和禁用此功能。我已经有代码禁用其他一些功能,它们看起来像这样:

    //Allow mock locations
    Settings.Secure.putInt(getContentResolver(),
            Settings.Secure.ALLOW_MOCK_LOCATION, 
            1);

    //Stay on while plugged in
    Settings.Global.putInt(getContentResolver(),
            Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 
            BatteryManager.BATTERY_PLUGGED_USB);

注意*我以root身份运行,因此我可以更改这些设置。所以要澄清一下,我的问题是如何更改设置验证应用程序,它位于何处?

2 个答案:

答案 0 :(得分:3)

您正在寻找Settings.Global.PACKAGE_VERIFIER_ENABLE。它似乎没有出现在文档站点上,但您可以在源代码中看到它。

定义:

  

android.provider.Settings.java

默认设置应用的示例用法:

  

com.android.settings.SecuritySettings.java

答案 1 :(得分:2)

感谢(赞成票)@Paradopolis和@Geobits这个问题和答案组合。 @Geobits给出了正确的答案。如果有人需要详细的总结和方法来获取/设置该选项的状态,我就发帖了。

PACKAGE_VERIFIER_ENABLE首先在api级别14定义,"verifier_enable"下的android.provider.Settings.Secure值。

API等级14(4.0.1_r1):

public static final String PACKAGE_VERIFIER_ENABLE = "verifier_enable";

它的值和定义的类在API级别17之后发生了变化。它的值在"package_verifier_enable"

下变为android.provider.Settings.Global

API等级17(4.2_r1):

public static final String PACKAGE_VERIFIER_ENABLE = "package_verifier_enable";

PACKAGE_VERIFIER_ENABLE属性是公共定义的,但它在定义时被注释隐藏。因此,我们无法将其用作Settings.Secure.PACKAGE_VERIFIER_ENABLESettings.Global.PACKAGE_VERIFIER_ENABLE,但我们可以使用其值。

如果有人需要确定是否已选中/取消选中包验证,或以编程方式检查/取消选中它可以使用以下课程:

public abstract class DeviceSettings {

    public enum SettingsCheckStatus {
        CHECKED, UNCHECKED, UNDEFINED
    }

    public static SettingsCheckStatus isVerifyAppsChecked(Context context) {
        // PACKAGE_VERIFIER_ENABLE is added after API level 14.
        // Its value changed at API level 17

        int packageVerifierEnabledAsInt = -1;
        if (Build.VERSION.SDK_INT >= 17) {
            packageVerifierEnabledAsInt = Settings.Global.getInt(context.getContentResolver(), "package_verifier_enable", -1);
        } else if (Build.VERSION.SDK_INT >= 14) {
            packageVerifierEnabledAsInt = Settings.Secure.getInt(context.getContentResolver(), "verifier_enable", -1);
        } else {
            // No package verification option before API Level 14
        }

        SettingsCheckStatus settingsCheckStatus = SettingsCheckStatus.UNDEFINED;
        switch (packageVerifierEnabledAsInt) {
        case 0:
            settingsCheckStatus = SettingsCheckStatus.UNCHECKED;
            break;
        case 1:
            settingsCheckStatus = SettingsCheckStatus.CHECKED;
            break;
        default:
            break;
        }

        return settingsCheckStatus;
    }

    /**
     * 
     * @param context
     * @param checked
     * @return
     * 
     * You must add <b>android.permission.WRITE_SECURE_SETTINGS</b> to your application's manifest file to use this method. 
     * Keep in mind, lint error checking may show an error on your manifest. <a href="http://stackoverflow.com/questions/13801984/permission-is-only-granted-to-system-app">See link to solve it.</a> <br>
     * Note that, this method <b>can only work on rooted devices or if your application is going to be a system app.<b>
     * 
     */
    public static boolean setVerifyAppsChecked(Context context, boolean checked) {
        boolean operationCompletedSuccessfully = false;
        int packageVerifierEnabledAsInt = checked ? 1 : 0;
        try {
            if (Build.VERSION.SDK_INT >= 17) {
                operationCompletedSuccessfully = Settings.Global.putInt(context.getContentResolver(), "package_verifier_enable", packageVerifierEnabledAsInt);
            } else if (Build.VERSION.SDK_INT >= 14) {
                operationCompletedSuccessfully = Settings.Secure.putInt(context.getContentResolver(), "verifier_enable", packageVerifierEnabledAsInt);
            } else {
                // No package verification option before API Level 14
            }
        } catch (Throwable t) {
            // Your device is not rooted or your app is not a system app.
        }

        return operationCompletedSuccessfully;
    }
}