我想让用户选择将手机的方向设置为自动,纵向或横向。这意味着整个手机的方向不仅仅是我的应用程序。每个其他应用程序也必须具有横向方向。这样做的前两个很简单
肖像:
Settings.System.putInt( this.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
汽车:
Settings.System.putInt( this.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
但我无法找到将其设置为横向的方法。我想将所有应用程序设置为不仅仅是我的应用程序。我在这里看到它可以完成,但它没有提供太多信息。 How to make Android system force LANDSCAPE for all apk?。有谁愿意解释或提供代码片段?
答案 0 :(得分:1)
以下代码可以强制您的屏幕进入横向模式。它类似于将其设置为其他模式。
public class MyService extends Service {
private boolean mViewAdded = false;
private WindowManager.LayoutParams mLayoutParams;
private View mOverlayView;
private WindowManager mWindowManager;
// Call this some where in your code
private void setLandscapeOrientation() {
mWindowManager = ((WindowManager)getSystemService("window"));
mOverlayView = new View(this);
mLayoutParams = new WindowManager.LayoutParams();
mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
mLayoutParams.width = 0;
mLayoutParams.height = 0;
mLayoutParams.flags = mLayoutParams.flags |
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
mLayoutParams.flags = mLayoutParams.flags &
~WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON &
~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
mLayoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
if (mViewAdded) {
mWindowManager.updateViewLayout(mOverlayView, mLayoutParams);
} else {
mWindowManager.addView(mOverlayView, mLayoutParams);
mViewAdded = true;
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mViewAdded) {
mWindowManager.removeView(mOverlayView);
mViewAdded = false;
}
}
}
答案 1 :(得分:-1)
在AndroidManifest.xml中,针对每个活动
android:screenOrientation="landscape"
它强制活动成为风景