Looking to enable and disable (toggle) ADB or USB

2019-02-07 08:39发布

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, su available, etc., but I cannot find a way to do the toggling.

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.

标签: android adb
4条回答
唯我独甜
2楼-- · 2019-02-07 09:23

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 Intent with Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS action to open developers options, where user can enable or disable 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_ENABLED was used.

查看更多
Summer. ? 凉城
3楼-- · 2019-02-07 09:28

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:

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.

First, I installed it regularly using eclipse, then 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
查看更多
4楼-- · 2019-02-07 09:29

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 -

Settings.Global.putInt(getContentResolver(),Settings.Global.ADB_ENABLED, 0); // 0 to disable, 1 to enable
查看更多
霸刀☆藐视天下
5楼-- · 2019-02-07 09:38

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.

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:

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

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

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

https://github.com/ramdroid/AdbToggleAccessLib

查看更多
登录 后发表回答