2

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();
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Pavel Kostal
  • 221
  • 5
  • 13
  • The main problem is, that setting "Settings.Global.DISPLAY_SIZE_FORCED" doesn't exist. I also tried Settings.Secure.DISPLAY_SIZE_FORCED, but still lot of people write about this setting on internet. – Pavel Kostal Sep 12 '19 at 20:06
  • In application Second Screen is used: Class.forName("android.view.IWindowManager") .getMethod("setForcedDisplaySize", int.class, int.class, int.class) .invoke(getWindowManagerService(), Display.DEFAULT_DISPLAY, width, height); – Pavel Kostal Sep 12 '19 at 20:13

1 Answers1

2

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"/>
ashakirov
  • 12,112
  • 6
  • 40
  • 40
  • How shall I initialize mContext? Could it be: Context mContext = this; ? – Pavel Kostal Sep 18 '19 at 13:08
  • 1
    It should be link to `Context`. So yes if `this` is `Activity`, `Service` or other class which parent is `Context`. I've added full code to my answer. – ashakirov Sep 18 '19 at 13:26
  • I got not error, but it won't change resolution. I tried it on simple new project. Any idea? – Pavel Kostal Sep 19 '19 at 08:53
  • 1
    I declare WRITE_SECURE_SETTINGS permission in manifest file. Then I install app. Then I grant permission with adb command. Then I run app. After that I restart phone. After restart my device resolution changed. Thats all I did. – ashakirov Sep 19 '19 at 09:16
  • After restart it works. Could you please write code for change dpi? Do you have any idea how to change resolution and dpi without restart? Anyway thank you very much for your help. – Pavel Kostal Sep 19 '19 at 12:46
  • I tried this: String valueDPI = "220"; Settings.Global.putString(getContentResolver(),"display_density_forced", valueDPI); It does nothing after restart. – Pavel Kostal Sep 19 '19 at 12:55