【问题标题】:Android - How to check if Developer option is enabledAndroid - 如何检查是否启用了开发人员选项
【发布时间】:2015-10-13 10:22:21
【问题描述】:

如何检查用户是否在其设备上启用了开发者选项? (没有激活 adb 通信或调试 USB 激活,我只需要知道是否启用了开发人员选项)。

我试过这个解决方案: How to check programmatically whether app is running in debug mode or not? 但这对我不起作用。 提前致谢

【问题讨论】:

  • 请向我们展示您到目前为止所做的尝试
  • @AbhinavSinghMaurya 查看修改后的问题
  • 设备开发人员选项启用与调试模式(您引用的解决方案)是两个不同的东西。确定是否启用了开发人员选项,以便生产应用程序可以像 Fortnight 那样为了安全而自行终止,这是我对我的游戏应用程序所做的事情,并且会推荐给任何不希望他们的知识产权被黑客入侵的人。

标签: android developer-tools android-developer-api


【解决方案1】:

试试这个:

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

【讨论】:

  • 对于 API 16 您可以使用:Settings.Secure.DEVELOPMENT_SETTINGS_ENABLED
  • 对于低于 16 的 API?
  • 我不确定 API startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS));
  • 如果为1表示已启用或有其他值要检查
【解决方案2】:

你应该使用 getInt 或 Settings.Global 中的另一个 和 DEVELOPMENT_SETTINGS_ENABLED

编辑: 在 API 17 以下,它是相同的,但使用 Settings.Secure

【讨论】:

【解决方案3】:

如果为 Android 4.1 或更高版本 (API 16) 的所有设备启用开发者模式,则返回 true,如果未在此类设备上启用开发者模式,则返回 false,并在所有早期 Android 设备(低至 1.0)上返回 false .

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:
      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;
                  }
      

      【讨论】:

        【解决方案6】:

        Cordova 或离子解决方案:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-01-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-11
          相关资源
          最近更新 更多