开发者

How to Change Screen Resolution Programmatically?

开发者 https://www.devze.com 2023-02-03 13:57 出处:网络
Let me tell you my problem. I want to change my screen resolution. I can change it in an application but it changes only ap开发者_运维知识库plication\'s

Let me tell you my problem. I want to change my screen resolution. I can change it in an application but it changes only ap开发者_运维知识库plication's screen. I wanna set system's resolution so it won't be important which application is running on front. My device's resolution is set as 1280 * 720 p. Can I make it 1260 * 680? If it requires to make changes in Android source code, I can. Just tell me where to change. Waiting for your help.


This thread on xda-developers should set you on the right track.


Searching too a valid answer to this, but I have a lead to the solution : WARNING Experimental buggy stuff :

/*
Requires android.permission.WRITE_SETTINGS
and android.permission.WRITE_SECURE_SETTINGS, thus it requires the app to be a system app.
 */
public void changeResolution(int x, int y){
    try { Class c = Class.forName("android.os.ServiceManager");
        try { Method method = c.getDeclaredMethod("checkService", String.class);
            try {
                IWindowManager mWindowManager = IWindowManager.Stub.asInterface((IBinder) method.invoke(null,Context.WINDOW_SERVICE));
                try { mWindowManager.setForcedDisplaySize(Display.DEFAULT_DISPLAY,x,y);
                } catch (RemoteException e) {e.printStackTrace();}
            }  catch (IllegalAccessException e) {e.printStackTrace();}
            catch (InvocationTargetException e) {e.printStackTrace();}
        } catch (NoSuchMethodException e) {e.printStackTrace();}
    } catch (ClassNotFoundException e) {e.printStackTrace();}
}

Add a reduced AIDL version of IWindowManager to your project : /app/src/main/aidl/android/view/IWindowManager.aidl

package android.view;
interface IWindowManager
{
    boolean startViewServer(int port);   // Transaction #1
    boolean stopViewServer();            // Transaction #2
    boolean isViewServerRunning();       // Transaction #3

    void setForcedDisplaySize(int displayId, int width, int height);
    void clearForcedDisplaySize(int displayId);
    void setForcedDisplayDensity(int displayId, int density);
    void clearForcedDisplayDensity(int displayId);
}

The app will require to be in the system apps folder. It does something for sure, but right now it also lead to severe bugs. Rebooting seems to cancel changes.

Waiting for feedback on this.


If you are using Windows and know how to use JNI, Microsoft provides C++ Win32 function calls to do this: ChangeDisplaySettingsEx() and EnumDisplaySettings().

0

精彩评论

暂无评论...
验证码 换一张
取 消