I want to read global settings programatically in Android.
Using ADB on AndroidStudio, the list command works perfectly:
adb shell settings list global
and returns the list of settings:
...
sound_trigger_detection_service_op_timeout=15000
sqlite_compatibility_wal_flags=
stay_on_while_plugged_in=7
subscription_mode=0
...
and also the put works as expected
adb shell settings put global stay_on_while_plugged_in 0
The problem is when I want to do it programmatically.
There are no exception but the "settings list global" command is not returning lines.
As you can see I tried also to ask the ACTION_MANAGE_WRITE_SETTINGS but doesn't works:
private void getCurrentSettings(){
if (Settings.System.canWrite(this)){
try {
String command = "settings list global";
Process shell = Runtime.getRuntime().exec(command);
shell.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(shell.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
Log.d("settings", line);
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:"+getApplicationContext().getPackageName()));
startActivity(intent);
}
}
UPDATE 30 Sep 2021:
Thanks to the comment by @TDG I can get the error of the exec command.
This is the error in case of list command: "settings list global"
Exception occurred while executing 'list':
java.lang.SecurityException: Permission Denial: getCurrentUser() from pid=32475, uid=10603 requires android.permission.INTERACT_ACROSS_USERS
at com.android.server.am.UserController.getCurrentUser(UserController.java:2168)
at com.android.server.am.ActivityManagerService.getCurrentUser(ActivityManagerService.java:18772)
at com.android.providers.settings.SettingsService$MyShellCommand.onCommand(SettingsService.java:259)
at android.os.BasicShellCommandHandler.exec(BasicShellCommandHandler.java:98)
at android.os.ShellCommand.exec(ShellCommand.java:44)
at com.android.providers.settings.SettingsService.onShellCommand(SettingsService.java:49)
at android.os.Binder.shellCommand(Binder.java:929)
at android.os.Binder.onTransact(Binder.java:813)
at android.os.Binder.execTransactInternal(Binder.java:1159)
at android.os.Binder.execTransact(Binder.java:1123)
This is the error in case of get command: "settings get global stay_on_while_plugged_in"
Exception occurred while executing 'get':
java.lang.SecurityException: Permission Denial: getCurrentUser() from pid=32124, uid=10603 requires android.permission.INTERACT_ACROSS_USERS
at com.android.server.am.UserController.getCurrentUser(UserController.java:2168)
at com.android.server.am.ActivityManagerService.getCurrentUser(ActivityManagerService.java:18772)
at com.android.providers.settings.SettingsService$MyShellCommand.onCommand(SettingsService.java:259)
at android.os.BasicShellCommandHandler.exec(BasicShellCommandHandler.java:98)
at android.os.ShellCommand.exec(ShellCommand.java:44)
at com.android.providers.settings.SettingsService.onShellCommand(SettingsService.java:49)
at android.os.Binder.shellCommand(Binder.java:929)
at android.os.Binder.onTransact(Binder.java:813)
at android.os.Binder.execTransactInternal(Binder.java:1159)
at android.os.Binder.execTransact(Binder.java:1123)