[Android]强制系统横屏

来源:互联网 发布:十年前流行的网络歌曲 编辑:程序博客网 时间:2024/05/17 06:01


该实现适用于强制整个手机所有APP及主页显示运行于横屏模式。


原文来自于StackOverflow:http://stackoverflow.com/questions/14587085/how-can-i-globally-force-screen-orientation-in-android


系统权限:

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


1)Service中实现:

orientationChanger = new LinearLayout(this);// Using TYPE_SYSTEM_OVERLAY is crucial to make your window appear on top// You'll need the permission android.permission.SYSTEM_ALERT_WINDOWWindowManager.LayoutParams orientationLayout = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 0, PixelFormat.RGBA_8888);// Use whatever constant you need for your desired rotationorientationLayout.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;WindowManager wm = (WindowManager) this.getSystemService(Service.WINDOW_SERVICE);wm.addView(orientationChanger, orientationLayout);orientationChanger.setVisibility(View.VISIBLE);

2)Activity中实现:

 wm = (WindowManager) content.getSystemService(Service.WINDOW_SERVICE);    orientationChanger = new LinearLayout(content);    orientationChanger.setClickable(false);    orientationChanger.setFocusable(false);    orientationChanger.setFocusableInTouchMode(false);    orientationChanger.setLongClickable(false);    orientationLayout = new WindowManager.LayoutParams(            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,            windowType, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,            PixelFormat.RGBA_8888);    wm.addView(orientationChanger, orientationLayout);    orientationChanger.setVisibility(View.GONE);    orientationLayout.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;    wm.updateViewLayout(orientationChanger, orientationLayout);    orientationChanger.setVisibility(View.VISIBLE);


0 0