寻找来启用和使用命令行或在应用程序禁用(切换)ADB或USB调试(Looking to enable

2019-06-26 00:49发布

我试图使ADB(USB调试),只有当我的应用程序正在运行,并禁用它,当我的应用程序是没有的。 我有充分的机会在手机和它植根, su可,等等,但我不能找到一种方法,做切换。

我试过到目前为止:

Process proc = Runtime.getRuntime().exec(new String [] { "su", "-c", "setprop", "persist.service.adb.enable", "0"});
proc.waitFor();
Process proc2 = Runtime.getRuntime().exec(new String [] { "su", "-c", "stop", "adbd"});
proc2.waitFor();

然而,这将导致手机瞬间进入一个重新启动循环。

Answer 1:

该工程的代码很简单:

Settings.Secure.putInt(getContentResolver(),Settings.Secure.ADB_ENABLED, 0); // 0 to disable, 1 to enable

编辑/更新:感谢@MohanT以下更新:

Settings.Global.putInt(getContentResolver(),Settings.Global.ADB_ENABLED, 0); // 0 to disable, 1 to enable

然而,应用程序必须是一个系统的应用程序才能够使用它。 为了摆脱不必签署它,建立一个自定义ROM等。我做了以下得到它是一个系统的应用程序。

首先,我安装了它经常使用Eclipse,那么亚行外壳:

> su
# mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
# cat /data/app/filename.apk > /system/app/filename.apk
# mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system
# reboot


Answer 2:

您可以通过以下接下来的步骤实现:

  1. 亚行检查当前是否启用。 要做到这一点,你可以使用相关的全局常量:

    • Settings.Global.ADB_ENABLED为API 17及以上。
    • Settings.Secure.ADB_ENABLED之前API 17。
  2. 使用隐式IntentSettings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS操作打开开发人员选项,在这里用户可以启用或禁用亚行。

代码示例:

Java的

public static final int API = Build.VERSION.SDK_INT;
public static final int ENABLED=1, DISABLED=0;

public static boolean adbEnabled(Context context){
    if(API>16)
       return ENABLED == Settings.Global.getInt(context.getContentResolver(), Settings.Global.ADB_ENABLED,DISABLED);
    else 
       return ENABLED == Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.ADB_ENABLED,DISABLED);
}

public void checkAdb(Context context){
    //if ADB disabled
    if(!adbEnabled(context)){
        //open developer options settings 
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
        context.startActivity(intent);
    }
}

科特林

val API = Build.VERSION.SDK_INT
val ENABLED = 1
val DISABLED = 0

fun adbEnabled(context: Context): Boolean {
    return if (API > 16)
        ENABLED == Settings.Global.getInt(context.contentResolver, Settings.Global.ADB_ENABLED, DISABLED)
    else
        ENABLED == Settings.Secure.getInt(context.contentResolver, Settings.Secure.ADB_ENABLED, DISABLED)
}
fun checkAdb(context: Context) {
    //if ADB disabled
    if (!adbEnabled(context)) {
        //open developer options settings
        val intent = Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS)
        context.startActivity(intent)
    }
}

仅供参考:

此前API 3(可能不再适用) Settings.System.ADB_ENABLED使用。



Answer 3:

克里斯的答案是正确的,除了在安全类的ADB_EN​​ABLED领域现在已经贬值和字符串已被转移到全球一流。 所以,你可以使用下面的命令 -

Settings.Global.putInt(getContentResolver(),Settings.Global.ADB_ENABLED, 0); // 0 to disable, 1 to enable


Answer 4:

从使用setprop并不总是可靠的我的经验,甚至可能与设置>应用程序>开发 - > USB调试选项冲突。 此外,如果USB调试启用,但亚行不工作,可能会刺激例如等,因此使用Settings.Secure.ADB_EN​​ABLED是首选的方式...加上你总是在状态面板的通知。

如果你不想经历的麻烦对系统分区上安装的应用程序时,只是想Settings.Secure.ADB_EN​​ABLED那么你可以而是依赖于另一个应用程序,已经在做这项工作访问:

这个应用程序被称为ADB切换。 您可以在谷歌播放找到它:

https://play.google.com/store/apps/details?id=com.ramdroid.adbtoggle

通过ADB切换访问USB调试设置的资料库,请访问:

https://github.com/ramdroid/AdbToggleAccessLib



文章来源: Looking to enable and disable (toggle) ADB or USB debugging using command line or in app
标签: android adb