在我的应用程序中控制默认的自动旋转屏幕

5

我的应用程序中有一个切换按钮。我想通过编程更改或控制默认设置,即自动旋转屏幕(设置>显示>自动旋转屏幕)。有人知道如何做到这一点吗?

4个回答

10

你在 Activity 中尝试过这个吗?

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

//This is the default value
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

之后,您可以使用以下内容来禁用自动方向调整:

public static void setAutoOrientationEnabled(ContentResolver resolver, boolean enabled)
{
  Settings.System.putInt(resolver, Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}

文档


我不仅想改变一个活动的方向,而是想改变设置。我想启用和禁用Android设置中的自动旋转屏幕。 - Santhosh_pulliman
退出应用程序后,设备不应自动旋转屏幕。 - Santhosh_pulliman

7
您可以使用这个:
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.USER ROTATION,user_rotation);

为轮换政策
user_rotation 0 -> ROTATION_0
user_rotation 1 -> ROTATION_90
user_rotation 2 -> ROTATION_180
user_rotation 3 -> ROTATION_270

请查看http://developer.android.com/reference/android/provider/Settings.System.html#USER_ROTATION以获取更多信息。

此外,还有menifest.xml设置。

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

3

您可以在清单文件中设置默认的旋转设置,例如:

<activity android:name=".MainTabActivity" android:screenOrientation="portrait">
</activity>

要通过程序更改方向,您需要调用Activity.setRequestedOrientation()


在我的应用程序退出后,我想通过我的代码将设备的自动旋转设置(设置>显示>自动旋转屏幕)设置为false。我该如何实现这一点? - Santhosh_pulliman

0
a1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        startActivity(new Intent(getApplicationContext(), MainActivity.class));
        android.provider.Settings.System.putInt(getContentResolver(),
                android.provider.Settings.System.USER_ROTATION,0);
    }
});
a2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        startActivity(new Intent(getApplicationContext(), MainActivity.class));
        android.provider.Settings.System.putInt(getContentResolver(),
                android.provider.Settings.System.USER_ROTATION,90);
    }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接