Android强制设置屏幕旋转方向 Force rotation

第一种方法:

首先检查有没有权限,没有就去申请。申请时会触发frameworks/base/services/core/java/com/android/server/wm/AlertWindowNotification.java里面

弹出可以覆盖view的权限窗口。

检查和处理的code如下:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            if (!Settings.canDrawOverlays(this))
            {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
                return;
            }
        }
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == OVERLAY_PERMISSION_REQ_CODE)
        {
            if (Settings.canDrawOverlays(this))
            {
                //Already has permission
            }
        }
    }

实际去锁定旋转和恢复的code如下:


public final static int STATE_DEFAULT = 0;
public final static int STATE_PORTRAIT = 1;
public final static int STATE_LANDSCAPE = 8;


WindowManager mWindowManager; View mView; WindowManager.LayoutParams lp; mWindowManager
= (WindowManager) getSystemService(Context.WINDOW_SERVICE); int iFlags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED; lp = new WindowManager.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, iFlags, PixelFormat.TRANSLUCENT ); mView = new View(this);
switch (rotation)
        {
            //Normal Operation
            case MainActivity.STATE_DEFAULT:
                if (mAdded == true) mWindowManager.removeView(mView);
                mAdded = false;
                break;
                
            //Force Rotation
            case MainActivity.STATE_LANDSCAPE:
            case MainActivity.STATE_PORTRAIT:
                lp.screenOrientation = rotation;
                if (mAdded == false)
                {
                    mWindowManager.addView(mView, lp);
                    mAdded = true;
                }else{
                    mWindowManager.updateViewLayout(mView, lp);
                }
                break;
        }

上面的方法在添加system权限后,可以直接获得权限,不再需要申请。

android:sharedUserId="android.uid.system"

 

第二种方法是仿照SystemUI里面检查旋转方向的方式,类似旋转屏幕后,把auto-rotation disable。

参照frameworks/base/core/java/com/android/internal/view/RotationPolicy.java里面

    /**
     * Returns true if rotation lock is enabled.
     */
    public static boolean isRotationLocked(Context context) {
        return Settings.System.getIntForUser(context.getContentResolver(),
                Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT) == 0;
    }

    /**
     * Enables or disables rotation lock from the system UI toggle.
     */
    public static void setRotationLock(Context context, final boolean enabled) {
        Settings.System.putIntForUser(context.getContentResolver(),
                Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0,
                UserHandle.USER_CURRENT);

        final int rotation = areAllRotationsAllowed(context) ? CURRENT_ROTATION : NATURAL_ROTATION;
        setRotationLock(enabled, rotation);
    }

    private static void setRotationLock(final boolean enabled, final int rotation) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
                    if (enabled) {
                            wm.freezeRotation(rotation);
                        } else {
                            wm.thawRotation();
                    }
                } catch (RemoteException exc) {
                    Log.w(TAG, "Unable to save auto-rotate setting");
                }
            }
        });
    }

因为直接用RotationPolicy中public的 setRotationLock(Context, final boolean)只能锁定当前已经旋转的屏幕,所以不如直接仿照这个private的

setRotationLock(final boolean, final int)去呼叫

import android.view.IWindowManager;
import android.view.Surface;
import android.view.WindowManagerGlobal;

if(value == LANDSCAPE) {
                AsyncTask.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
                            //if (enabled) {
                                    wm.freezeRotation(Surface.ROTATION_90);
/*                                } else {
                                    wm.thawRotation();
                            }*/
                        } catch (RemoteException exc) {
                            Log.w(TAG, "Unable to save auto-rotate setting");
                        }
                    }
                });
            }else {
                AsyncTask.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
                            wm.thawRotation();
                        } catch (RemoteException exc) {
                            Log.w(TAG, "Unable to save auto-rotate setting");
                        }
                    }
                });
            }

这个需要在源码里面编译,不然只能用反射。

 

Android强制设置屏幕旋转方向 Force rotation

上一篇:Appium.01——环境搭建


下一篇:安卓操作的一些问题解决