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.
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
You can achieve by following next steps:
Check whether ADB is currently enabled. To do that you can use a relevant global constant:
Settings.Global.ADB_ENABLED for API 17 and above.
Settings.Secure.ADB_ENABLED prior to API 17.
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.
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
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
Related
Question about changing the parameters of the transition to doze mode
I have a non-rooted Android 12 device
There are a number of parameters for changing the transition time in doze mode:
inactive_to
motion_inactive_to
light_after_inactive_to
If you change these parameters through the PC using ADB, then the parameters are set. For instance:
adb shell device_config put device_idle inactive_to 2592000000
The problem is that after a reboot, the settings are reset.
Tried to change them directly in the program
// val command = arrayOf("/system/bin/device_config", "put", "device_idle", "motion_inactive_to", "2592000000")
val command = arrayOf("/system/bin/settings", "put", "global", "device_idle_constants", "light_after_inactive_to", "2592000000")
Log.d("ADB", "Start ${command.joinToString(separator = " ")}")
val process = Runtime.getRuntime().exec(command)
val bufReader = BufferedReader(InputStreamReader(process.errorStream))
val log = StringBuilder()
var line: String?
line = bufReader.readLine()
while (line != null) {
log.append(line + "\n")
line = bufReader.readLine()
}
Log.d("ADB", "Command: $log")
But the first command is answered:
“cmd: Can't find service: "device_config"”
And the second command gives an error:
Permission Denial: getCurrentUser() from pid=28435, uid=10245 requires android.permission.INTERACT_ACROSS_USERS
After searching for information about the INTERACT_ACROSS_USERS permission, I understand that it is necessary for it to make the application system. And for this I need to root the device.
Is there any other way to change the parameters of doze mode or disable it altogether?
It's confusing that you can run a command with adb on a non-rooted device, but you can't directly in the program.
Is there any other way to change the parameters of doze mode or disable it altogether?
No. If there were, it would have been pointless to add Doze mode, as every developer would opt out of it.
It's confusing that you can run a command with adb on a non-rooted device, but you can't directly in the program.
Different users/processes having different privileges has been a common concept in operating systems for at least 30 years.
I'm trying to run adb commands programmatically from inside an Android priviledged app (apk file uploaded to priv-app folder, privapp-permissions-platform.xml file uploaded to /etc/permissions/.
private fun executeAdbCommand() {
val commandStart = "am start -n com.abc.bcd/com.abc.bcd.MainActivity"
val process = Runtime.getRuntime().exec(commandStart)
val bufferedReader = BufferedReader(InputStreamReader(process.inputStream))
val commandBattery = "dumpsys battery set level 20"
val processBattery = Runtime.getRuntime().exec(commandBattery)
val bufferedReaderBattery = BufferedReader(InputStreamReader(processBattery.inputStream))
val commandBack = "input keyevent ${KeyEvent.KEYCODE_BACK}"
val processBack = Runtime.getRuntime().exec(commandBack)
val bufferedReaderThree = BufferedReader(InputStreamReader(processBack.inputStream))
}
The first two commands produce no result. The third one is actually pressing the Back Button.
When I run the first two commands from a PowerShell windows they are both executed correctly:
adb shell am start -n com.abc.bcd/com.abc.bcd.MainActivity is starting the mentioned activity,
adb shell dumpsys battery set level 20 is setting the emulator's battery to 20%.
I am targeting Android 10 (API level 29) - is this the expected behaviour of the system? What is the particular reason the first two commands are getting discarded (no error log is generated) and the third one is getting executed? Is there any way of getting the first two commands running (might this be a missing permission or something similar)?
After reading through a bunch of stale guides and stackoverflows, I was able to usb adb to install an apk as a system app in /system/priv-app that successfully toggles AirplaneMode in Android oreo:
// method in Activity, called via click listener on a Button
private void setMobileRadioEnabled_Option1(boolean enabled) {
android.content.Context context = this;
int value = enabled ? 0 : 1;
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
Settings.System.putInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, value);
} else {
Settings.Global.putInt(
context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, value);
}
}
Permissions in AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
Standard release build in AndroidStudio with custom signing keys, installed via:
adb root
adb remount
adb push app-release.apk /system/priv-app
adb shell chmod 644 /system/priv-app/app-release.apk
adb reboot
On reboot, the app is installed and I can run it without issue.
I check in the notifications drawer / status bar what things are like to start with:
I then click my Button in the app, and check what happens:
As you can see, airplane mode seems to be successfully enabled based on the status of the airplane mode icon. But wifi and cellular data continue to be connected, and the status bar doesn't replace the text "Android" with "Airplane mode". In this state, if I hop over to chrome, I can clearly load websites I've never visited before. So airplane mode doesn't in fact seem to be actually on.
What am I doing wrong? I expect turning on airplane mode via System.putInt() to have the same effect as tapping the airplane mode tile in the status bar. No exceptions or useful error information spitting to logcat when I execute the code.
Checking this answer it seems that you need to send a broadcast to notify that you changed the airplane mode.
The broadcast should be:
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", < current value of the airplane mode >);
sendBroadcast(intent);
Alternatively to jonathanrz's additional code above, which I believe is closer to canonical, I found that the following worked in place of the Settings.System.putInt() code and did not require sending the intent (or adding the permission(s) necessary to send it), at least on Oreo. I created it by merging a few answers and offhand comments from other posts, particularly an answer sketch hidden in a comment by "Navas pk" on Toggle airplane mode in Android:
private void setMobileRadioEnabled_Option2(boolean enabled) {
try {
final ConnectivityManager mConnectivityManager = (ConnectivityManager) getSystemService(android.content.Context.CONNECTIVITY_SERVICE);
final Class mClass = Class.forName(mConnectivityManager.getClass().getName());
final Method setAirplaneMode = mClass.getDeclaredMethod("setAirplaneMode", Boolean.TYPE);
setAirplaneMode.setAccessible(true);
setAirplaneMode.invoke(mConnectivityManager, !enabled);
} catch (Exception e) {
e.printStackTrace();
}
}
I want to watch the layout of App without boring tap and tap.
I tried adb shell setprop debug.layout true but didn't work unless reboot or open setting.
This may caused by setting haven't update.
I tried to writing a little App with code SystemProperties.set("debug.layout", "true") , no use too.
Maybe the app's authority…
Sorry for my poor English and appreciation for help :p
This works for me:
adb shell setprop debug.layout true
adb shell service call activity 1599295570
After we have enabled Show layout bounds using adb shell setprop debug.layout true, we have to poke the SystemProperties to see the changes
as Show layout bounds QS Tiles does:
#Override
public void onClick() {
setIsEnabled(getQsTile().getState() == Tile.STATE_INACTIVE);
new DevelopmentSettings.SystemPropPoker().execute(); // Settings app magic
refresh();
}
Here's the original method from AOSP source:
public static class SystemPropPoker extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
String[] services = ServiceManager.listServices();
for (String service : services) {
IBinder obj = ServiceManager.checkService(service);
if (obj != null) {
Parcel data = Parcel.obtain();
try {
obj.transact(IBinder.SYSPROPS_TRANSACTION, data, null, 0);
} catch (RemoteException e) {
} catch (Exception e) {
Log.i(TAG, "Someone wrote a bad service '" + service
+ "' that doesn't like to be poked: " + e);
}
data.recycle();
}
}
return null;
}
}
The number 1599295570 is called SYSPROPS_TRANSACTION
Reference: https://github.com/dhelleberg/android-scripts/blob/master/src/devtools.groovy
UPDATED: I created this app to add many developer toggle to the quick settings for Android 7.0+
You don't need to start the settings app. Just exit your app, set the property, and start your app.
adb shell am force-stop com.company.appname ; adb shell setprop debug.layout true ; adb shell monkey -p com.company.appname -c android.intent.category.LAUNCHER 1
I found a DevelopQuickSetting tool can easily make this.
https://github.com/kyze8439690/DevelopQuickSetting
and the core code translate to adb shel is:
adb shell setprop debug.layout true
adb shell service check SurfaceFlinger
This tool works fine.
You need to install groovy before launch this program.
https://github.com/dhelleberg/android-scripts
Here is my fish shell function that will also help you to choose from multiple devices with fzf. save it in ~/.config/fish.functions/ and use it like this:
and if you have more than one device it will prompt to select one with fzf
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.
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
You can achieve by following next steps:
Check whether ADB is currently enabled. To do that you can use a relevant global constant:
Settings.Global.ADB_ENABLED for API 17 and above.
Settings.Secure.ADB_ENABLED prior to API 17.
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.
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
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