感谢(赞成)@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_ENABLE
or 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;
}
}