How to change resolution(size) and density with Android Code, Runtime.getRuntime().exec(...) doens't works

8.2k Views Asked by At

I would like to toggle resolution and density of my phone using my application (don't want to use ADB).

  • When I use Runtime.getRuntime().exec("wm density 220") it doesn't work on AVD nor at my rooted phone Galaxy S8 with Custom ROM.
  • When I use Runtime.getRuntime().exec("wm size 1080x1920") it changes resolution on my Galaxy S8 but not on AVD.

I granted android.permission.WRITE_SECURE_SETTINGS to all my devices.

What I tried already:

  1. With super user privileges granted via (Runtime.getRuntime().exec("su"), I still can't change density with my app
  2. I can change density and size (resolution) with adb commands on any device
  3. I can change size(resolution) with application Tasker (this application has android.permission.WRITE_SECURE_SETTINGS) but it isn't possible to change density. Change density in Tasker is possible only with root.
  4. Application Second Screen (has also android.permission.WRITE_SECURE_SETTINGS and is possible to download source code on Github) could change resolution and density on my AVD and on my Galaxy S8 without ROOT
  5. With application Terminal Emulator I could change size and density only with ROOT
  6. Application Second Screen and also it is mentioned on this site: http://nomone.com/2016/10/11/modifying-android-system-settings/ using code "Settings.Global.putString" this should fix my problem, but I don't know how to use it. Could you please help me?
//Part of the code used from http://nomone.com/ and is also used in app
//Second Screen, there they don't use Runtime.getRuntime.exec("...")
Settings.Global.putString(
mContext.getContentResolver(),
Settings.Global.DISPLAY_SIZE_FORCED, width + "," + height);

//this is part of the code with Runtime.getRuntime
Process process = Runtime.getRuntime().exec("wm density 220");
process.waitFor();
1

There are 1 best solutions below

6
ashakirov On

Settings.Global.DISPLAY_SIZE_FORCED exists, but it is not accessible. You can use actual value of that String constant

enter image description here

so code would be:

Settings.Global.putString(mContext.getContentResolver(),
                "display_size_forced", 
                 width + "," + height);

This code works on my android 9 device if I grant WRITE_SECURE_SETTINGS permission with adb command via terminal:

adb pm grant <package name> android.permission.WRITE_SECURE_SETTINGS 

I'm surprised but screen resolution changes to new one without root. I don't know how that can be done without adb command.

EDIT: Thats how I call this method:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Settings.Global.putString(getContentResolver(),
                "display_size_forced",
                width + "," + height);
    }
}

In manifest file add permission:

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