我使用此代码来检测手机上是否启用了开发人员选项:
int developerOptions = Settings.Secure.getInt(this.getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED , 0);
Run Code Online (Sandbox Code Playgroud)
但是,我对此进行了测试,它在少数设备上返回错误的值(一些华为手机和其他......)
是否有另一种完整的方法来检测设备中是否启用了开发人员选项?
我尝试了这个,但它不起作用(无论如何我不想使用该方法,因为它不优雅,我只是在测试):
try
{
startActivityForResult(new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS), 8080);
finishActivity(8080);
// Developer options enabled
}
catch (Exception e)
{
// Developer options disabled
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序的最低 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;
}
Run Code Online (Sandbox Code Playgroud)
您的代码很接近,但没有考虑 Build.TYPE.equals("eng") ?1:0)
| 归档时间: |
|
| 查看次数: |
2737 次 |
| 最近记录: |