12

my question is on an Android phone how can I check if the usb debugging flag is enabled or not programmatically? in my application I want to show the status of usb debugging and I want to get it programmatically

How can I get if usb debugging is enabled programmatically?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Neji
  • 6,591
  • 5
  • 43
  • 66

2 Answers2

19

Try this:

if(Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.ADB_ENABLED, 0) == 1) {
    // debugging enabled 
} else {
    //;debugging does not enabled
}
DrMartens
  • 441
  • 5
  • 11
  • I notice that ADB_ENABLED now has a line through it, with a message that 'ADB_ENABLED' is deprecated as of Android 17 (Jelly Bean)' on the mouseover. I don't see any promising suggestions on the autocomplete list after 'Secure.' Anyone know what the current syntax is for this? – Androidcoder Feb 17 '20 at 16:07
  • 2
    Try Settings.Global instead of Settings.Secure on android API levels 17+ – satur9nine Mar 10 '20 at 17:49
  • if(Settings.Secure.getInt(this.getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) == 1) – harikrishnan Oct 10 '22 at 06:20
0

Simple:

   boolean isDebuggable =  ( 0 != ( getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE ));

And if you want to check if it is connected:

if (!Debug.isDebuggerConnected()){
   //Yes, it is.
}
Vansuita Jr.
  • 1,949
  • 17
  • 18
  • 1
    The question is about whether or not USB debugging is enabled, not if the app is debuggable or being debugged. Still useful though – Paul T. Jan 16 '19 at 16:33