Enable USB debugging (under settings/applications/development) programmatically from within an app

后端 未结 5 1209
北海茫月
北海茫月 2020-12-11 03:23

Is it possible to enable USB debugging (under settings/applications/development) programmatically from within my app?

I was looking at Permission.WRITE_SETTING

相关标签:
5条回答
  • 2020-12-11 03:49

    First: Your app must be a system app

    This line of code may help:

    Settings.Global.putInt(getContentResolver(), Global.ADB_ENABLED, 1);

    and you need this permission:

    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

    but after adding this permission in manifest you will get this error: Permission is only granted to system apps

    which means your app must be a system app.

    0 讨论(0)
  • 2020-12-11 03:53

    It's not possible without using your own custom firmware that grants access to the security settings. See thread here: http://groups.google.com/group/android-developers/browse_frm/thread/953c6f0eb0fa9bed#

    0 讨论(0)
  • 2020-12-11 04:06

    You can enable adb programmatically by requesting WRITE_SECURE_SETTINGS in manifest and granting it over adb shell:

    adb shell pm grant your.package.name android.permission.WRITE_SECURE_SETTINGS
    

    Then you can enable adb on API 17 and above by calling:

    Settings.Global.putString(mContext.getContentResolver, Settings.Global.ADB_ENABLED,"1");
    

    For API 16 to API 3 call:

    Settings.Secure.putString(mContext.getContentResolver, Settings.Secure.ADB_ENABLED,"1");
    

    To disable adb replace "1" with "0" in commands

    0 讨论(0)
  • 2020-12-11 04:11

    If your device has root, and API level > 17 for enable adb:

    Runtime.getRuntime().exec(new String[] {"su", "-c", "settings put global adb_enabled 1"});
    

    or (depends of su implementation)

    Runtime.getRuntime().exec(new String[] {"su", "settings put global adb_enabled 1"});
    

    For disable - change last argument from 1 to 0

    0 讨论(0)
  • 2020-12-11 04:13

    You will need root permissions to do so from an app.

    That said, it is possible to enable ADB by executing the following terminal commands:

    setprop persist.service.adb.enable 1
    start adbd
    

    This blog post gives an excellent example of how to execute these commands with escalated permissions through su.

    0 讨论(0)
提交回复
热议问题