Android 希望使用命令行或应用程序启用和禁用(切换)ADB 或 USB 调试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11314145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:54:05  来源:igfitidea点击:

Looking to enable and disable (toggle) ADB or USB debugging using command line or in app

androidadb

提问by Chris

I'm trying to enable ADB (USB debugging) only when my application is running and disabling it when my application is not. I have full access to the phone and it is rooted, suavailable, etc., but I cannot find a way to do the toggling.

我试图仅在我的应用程序正在运行时启用 ADB(USB 调试),并在我的应用程序未运行时禁用它。我可以完全访问手机,并且它已植根、su可用等,但我找不到进行切换的方法。

What I've tried so far:

到目前为止我尝试过的:

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

This however causes the phone to enter a reboot loop instantaneously.

然而,这会导致手机立即进入重启循环。

回答by Chris

The code that works is easy:

有效的代码很简单:

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

Edit/Update: Thanks to @MohanT for the following update:

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

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

However, the app needs to be a system app to be able to use this. To get out of having to sign it, build a custom rom, etc.. I did the following to get it to be a system app.

但是,该应用程序必须是系统应用程序才能使用它。为了摆脱签名,构建自定义 rom 等。我执行了以下操作以使其成为系统应用程序。

First, I installed it regularly using eclipse, then adb shell:

首先,我使用 eclipse 定期安装它,然后使用 adb shell:

> 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

回答by Nikita Kurtin

You can achieve by following next steps:

您可以通过以下步骤实现:

  1. Check whether ADB is currently enabled. To do that you can use a relevant global constant:

  2. Use implicit Intentwith Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGSaction to open developers options, where user can enable or disable ADB.

  1. 检查当前是否启用了 ADB。为此,您可以使用相关的全局常量:

  2. 使用implicit Intentwith Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGSaction 打开开发人员选项,用户可以在其中启用或禁用ADB。

Code example:

代码示例:

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

Kotlin

科特林

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

Just for reference:

Prior to API 3 (probably no longer relevant) Settings.System.ADB_ENABLEDwas used.

仅供参考:

在 API 3(可能不再相关)Settings.System.ADB_ENABLED之前使用。

回答by mohan.t

Chris's answer is correct except the ADB_ENABLED field in Secure class has been depreciated and the string has been moved to Global class. So you can use below command -

Chris 的回答是正确的,只是 Secure 类中的 ADB_EN​​ABLED 字段已折旧并且字符串已移至 Global 类。所以你可以使用下面的命令 -

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

回答by ramdroid

From my experiences using setprop doesn't always work reliable, and might even conflict with the Settings > Applications > Development > USB Debugging option. Besides that it might be irritating for instance if USB debugging is enabled but adb doesn't work etc. Therefore using Settings.Secure.ADB_ENABLED is the prefered way... plus you always have the notification in the status panel.

根据我使用 setprop 的经验,它并不总是可靠的,甚至可能与 Settings > Applications > Development > USB Debugging 选项冲突。除此之外,如果启用了 USB 调试但 adb 不起作用等,它可能会令人恼火。因此,使用 Settings.Secure.ADB_EN​​ABLED 是首选方式......此外,您总是在状态面板中收到通知。

If you don't want to go through the hassle to install your app on system partition when only trying to access Settings.Secure.ADB_ENABLED then you can instead rely on another app that is doing this work already:

如果您不想在仅尝试访问 Settings.Secure.ADB_EN​​ABLED 时在系统分区上安装应用程序的麻烦,那么您可以依赖另一个已经在执行此工作的应用程序:

This app is called ADB Toggle. You can find it in Google Play:

这个应用程序叫做 ADB Toggle。您可以在 Google Play 中找到它:

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

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

The library to access USB debug settings via ADB Toggle is available here:

通过 ADB Toggle 访问 USB 调试设置的库可在此处获得:

https://github.com/ramdroid/AdbToggleAccessLib

https://github.com/ramdroid/AdbToggleAccessLib