19

Is there any way to access device settings programmatically?

For Example: Settings > Developer Options > USB debugging > True/False

Thanks in advance.

Edit: USB debugging is just an example. Can I access every settings which is exist in Settings in device.

auy
  • 283
  • 1
  • 4
  • 11

6 Answers6

16

Settings.Secure.putInt(getActivity().getContentResolver(),Settings.Secure.ADB_ENABLED, 1);

But you need root access to do this. Only system apps have permission to change secure settings

Edit: Try this to Show all ANRs

Settings.Secure.putInt(ContentResolver,Settings.Secure.ANR_SHOW_BACKGROUND,0);

and for Don't keep activities

ActivityManagerNative.getDefault().setAlwaysFinish(true)

but ActivityManagerNative is hidden in standard api set. you will need to do either reflection or use this method to get android.jar. Again I guess this needs root access

nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • Thanks. This code works but I wrote USB debugging as an example. Real question is, which settings are accessible. Can I access "Don't keep activities" or "Show all ANR's" also? – auy Aug 16 '12 at 10:45
  • `Settings.Secure.putInt(getActivity().getContentResolver(),Settings.Secure.ADB_ENABLED, 1);` won't work by itself as you need to sign your app with an Android platform certificate, your app has the appropriate configs in `AndroidManifest.xml`, and your app is installed in the `/system/app` programmatically or manually. You cannot even do this with a rooted device. – ChuongPham Dec 10 '13 at 14:22
  • Works nice, but on my device "putInt" deletes an old entry and creates a new one. I looked into the database via adb shell and sqlite3. (data/data/com.android.providers.settings/databases/settings.db) The "changed" name-value-pair has a new _id after executing putInt. Does the changed _id has any side effects? – bnGG Mar 08 '17 at 15:37
5

Try as:

 int adb = Settings.Secure.getInt(context.getContentResolver(),
  Settings.Secure.ADB_ENABLED, 0);
 // toggle the USB debugging setting
 adb = adb == 0 ? 1 : 0;
 Settings.Secure.putInt(context.getContentResolver(), 
Settings.Secure.ADB_ENABLED, adb);

In AndroidManifest.xml:

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

and finally look at this example

http://code.google.com/p/secure-settings-widget/source/browse/src/com/lieryan/adbwidget/ADBAppWidgetProvider.java?r=eb23b2d6989ccade05f095806db5888f1ba62737

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • 1
    The above won't work by itself as you need to sign your app with an Android platform certificate, your app has the appropriate configs in `AndroidManifest.xml`, and your app is installed in the `/system/app` programmatically or manually. – ChuongPham Dec 10 '13 at 14:21
3

I know that this question is old but is interesting.

Nandeesh's solution works well. But it does not work if you want to change certain settings such as USB Debugging. You have to use the following permission.

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

But, it doesn't work with non system apps. However, it is POSSIBLE to do this if you have root access. You can try this solution where the author solves the exact same thing.

You can also try https://github.com/alseambusher/adb-toggle

leet
  • 933
  • 1
  • 13
  • 27
2

If you app is System app then you Simply do this to enable/disable USB Debugging

Settings.Secure.putInt(getActivity().getContentResolver(),Settings.Secure.ADB_ENABLED, 1);

You also need root access to do this. I have rooted device and I just simply execute SU to enable/disable Usb Debugging.

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();

Good thing is you don't need to place your application in (/system/priv-app/) Your simple app i.e. (/data/app) can do this.

Hanan
  • 444
  • 4
  • 16
0

You can't turn on the USB Debugging unless your app is system app.

VishalKale
  • 758
  • 7
  • 22
0

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.

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78