3

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

在 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 身份运行,因此我可以更改这些设置。所以澄清一下,我的问题是如何更改设置验证应用程序,它位于哪里?

4

2 回答 2

3

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

定义:

android.provider.Settings.java

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

com.android.settings.SecuritySettings.java

于 2013-08-01T16:27:22.127 回答
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_ENABLEor Settings.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;
    }
}
于 2013-10-31T09:29:12.760 回答