Android - 如何检查开发者选项是否已启用

16

请展示一下您目前尝试过的内容。 - Abhinav Singh Maurya
@AbhinavSinghMaurya 请查看修改后的问题。 - user1432059
设备开发者选项启用与调试模式(您提到的解决方案)是两个不同的事情。确定开发者选项是否启用,以便生产应用程序可以像Fortnight一样自我终止以保证安全性,是我在我的游戏应用程序中执行的操作,并且我建议任何不想让其知识产权被盗的人都这样做。 - Androidcoder
7个回答

26

试试这个:

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

1
对于API 16,您可以使用:Settings.Secure.DEVELOPMENT_SETTINGS_ENABLED - JoGe
小于16的API? - user1432059
我不确定 API < 16 是否支持此功能。另一种方法是使用 try-catch 块,如果出现错误,则打开首选项页面。startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS)); - JoGe
1
如果它是1,表示它已启用或有另一个值可供检查。 - Mahmoud Mabrok

4

但是DEVELOPMENT_SETTINGS_ENABLED是在API 17中引入的。我如何在之前的API级别上检查它? - user1432059
1
你可以在API 17以下的这个链接中找到它:http://developer.android.com/reference/android/provider/Settings.Secure.html - Will Bobo

3
Kotlin解决方案:
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
fun isDevMode(context: Context): Boolean {
    return when {
        Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN -> {
            Settings.Secure.getInt(context.contentResolver,
                Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0
        }
        Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN -> {
            @Suppress("DEPRECATION")
            Settings.Secure.getInt(context.contentResolver,
                Settings.Secure.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0
        }
        else -> false
    }
}

如果使用 Kotlin,您还可以将其作为扩展函数实现。 - Ishaan Kumar
1
@IshaanKumar 当然,就像每个函数一样。你是在问我如何在这里做吗? - android developer

2
这里有一个方法,如果所有Android 4.1或更高版本(API 16)的设备都启用了开发者模式,则返回true;如果这些设备上未启用开发者模式,则返回false;对于早期的Android设备,从1.0开始,都返回false。请保留HTML标签。
        @android.annotation.TargetApi(17) public boolean isDevMode() {
            if(Integer.valueOf(android.os.Build.VERSION.SDK) == 16) {
                return android.provider.Settings.Secure.getInt(getApplicationContext().getContentResolver(),
                        android.provider.Settings.Secure.DEVELOPMENT_SETTINGS_ENABLED , 0) != 0;
            } else if (Integer.valueOf(android.os.Build.VERSION.SDK) >= 17) {
                return android.provider.Settings.Secure.getInt(getApplicationContext().getContentResolver(),
                        android.provider.Settings.Global.DEVELOPMENT_SETTINGS_ENABLED , 0) != 0;
            } else return false;
        }

-1

我搜索了很多方法,最终找到了一个方法,它在Android Studio中运行良好,在应用程序中也能正常工作,具体如下:

startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS));

这将打开应用程序设置页面。这怎么回答问题了呢? - Rohit Singh

-1

Cordova或Ionic解决方案:

https://github.com/Secullum/cordova-plugin-devoptionschecker

document.addEventListener('deviceready', onDeviceReady, false);

function onDeviceReady() {
    cordova.plugins.devoptionschecker.check(successCallback, errorCallback);
}

function successCallback(result) {
    // json object which returns devOptionsEnabled: true - enabled, false - disabled
    console.log(result);
}

function errorCallback(error) {
    console.log(error);
}

-1
It's Simple to Find -- Developer Mode is On or Not!!!
Java solution:   

 if (PreferenceHelper.isDevMode(context)) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("Please Turn Off Developer Option \n" +
                        " \n" +
                        " Go to Settings > Search developer options and toggle them off.");
                builder.setCancelable(false);
                builder.setNegativeButton(" Ok ", (dialog, which) -> {
                    dialog.dismiss();
                    finish();
                });
                builder.setPositiveButton(" Turn Off ", (dialog, which) -> {
                    startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS));
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
                alertDialog.getButton(android.app.AlertDialog.BUTTON_POSITIVE).setTextColor(Color.parseColor("#37367C"));
                return;
            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接