我使用此代码来检测是否在电话上启用了开发人员选项:
int developerOptions = Settings.Secure.getInt(this.getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED , 0);
但是,我对此进行了测试,并在少数设备(某些华为电话等)上返回了错误的值。
是否有另一种充分证明方式来检测设备中是否启用了开发人员选项?
我尝试了此方法,但它不起作用(无论如何,我都不希望使用该方法,因为它不美观,我只是在进行测试):
try
{
startActivityForResult(new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS), 8080);
finishActivity(8080);
// Developer options enabled
}
catch (Exception e)
{
// Developer options disabled
}
我的应用的最低API级别为21。
您无法做到比Android本身更加安全:
public static boolean isDevelopmentSettingsEnabled(Context context) {
final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
final boolean settingEnabled = Settings.Global.getInt(context.getContentResolver(),
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED,
Build.TYPE.equals("eng") ? 1 : 0) != 0;
final boolean hasRestriction = um.hasUserRestriction(
UserManager.DISALLOW_DEBUGGING_FEATURES);
final boolean isAdmin = um.isAdminUser();
return isAdmin && !hasRestriction && settingEnabled;
}
您的代码很接近,但是没有考虑Build.TYPE.equals(“ eng”)吗?1:0)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句