How to access device settings programmatically?

前端 未结 6 1660
礼貌的吻别
礼貌的吻别 2020-12-05 05:59

Is there any way to access device settings programmatically?

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

Thanks in advance.

<
相关标签:
6条回答
  • 2020-12-05 06:07

    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-05 06:12

    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.

    0 讨论(0)
  • 2020-12-05 06:14

    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

    0 讨论(0)
  • 2020-12-05 06:18

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

    0 讨论(0)
  • 2020-12-05 06:19

    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

    0 讨论(0)
  • 2020-12-05 06:29

    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

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