简体   繁体   中英

Foolproof way to detect if developer options are enabled?

I use this code to detect if developer options are enabled on a phone or not:

int developerOptions = Settings.Secure.getInt(this.getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED , 0);

However, I tested this and it returns the wrong value on a small number devices (some Huawei phones and others...)

Is there another full proof way to detect if developer options are enabled in a device?

I tried this but it doesn't work (I don't want to use that method anyway because it's not elegant, I'm just testing around):

try 
{
    startActivityForResult(new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS), 8080);
    finishActivity(8080);
    // Developer options enabled
} 
catch (Exception e) 
{
    // Developer options disabled
}

My app's minimum API level is 21.

I've taken a look at this question and other similiar ones on SO but I didn't find a fullproof solution. This is not a duplicate question.

You can't do it any more foolproof than Android itself does it:

 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;
}

Your code was close, but didn't account for Build.TYPE.equals("eng")? 1: 0)

Min API 17 tested on emulator

public boolean isDeveloperModeEnabled(){
    if (Integer.valueOf(android.os.Build.VERSION.SDK) >= 17) {
        return android.provider.Settings.Secure.getInt(getActivity().getApplicationContext().getContentResolver(),
                android.provider.Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
    }
    return false;
}

Try the code below:

int devOptions = Settings.Secure.getInt(this.getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED,
Build.TYPE.equals("eng") ? 1 : 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM