2

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.

Bandy
  • 161
  • 12
  • Can you let me know what `String` do you get if you get or log the value of `Settings.Global.DEVELOPMENT_SETTINGS_ENABLED` on Huawei phone, compared to other working phones? If I am correct, it might due to the **localization of Chinese phones**. – Harry Timothy Aug 31 '20 at 19:06
  • @HarryTimothy I don't personally own a Huawei unfortunately, my users reported this issue to me. – Bandy Aug 31 '20 at 20:39

3 Answers3

7

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)

Technologeeks
  • 7,674
  • 25
  • 36
  • You dont need that part.. UserManagerService isn't meant to be visible. You need just that: boolean settingEnabled = Settings.Global.getInt(context.getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, Build.TYPE.equals("eng") ? 1 : 0) != 0; - The rest just reverses the true value if the calling user is not an admin user. – Technologeeks Aug 31 '20 at 21:01
  • Thanks for the help. However, on my emulator (Pixel 2 API 24), the code doesn't detect that developer options are enabled. You can try it. (Go to 'about device' and tap on 'build number' 7 times to activate developer options) Try it on `Pixel 2 API 24` specifically. – Bandy Sep 01 '20 at 11:02
  • The emulator isn't 100% the same as Android, so I wouldn't put it as the test case. The code I gave you literally IS the code from the developer options settings, meaning this is how AOSP itself knows whether or not dev opts are enabled. For specific devices (or emulator) which fall out of this generalization, I'd suggest decompiling their settings app and comparing it to the AOSP sources. – Technologeeks Sep 01 '20 at 12:03
  • Okay, thanks! I'll test this code with my users and I'll get back to you. – Bandy Sep 01 '20 at 12:11
  • I'll award you the bounty. However, I'm still waiting for feedback from my users. – Bandy Sep 06 '20 at 18:26
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;
}
TRK P
  • 366
  • 2
  • 9
0

Try the code below:

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