且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

以编程方式启用和禁用自动旋转?

更新时间:2023-02-13 10:26:20

这应该为您解决问题:

    import android.provider.Settings;
    public static void setAutoOrientationEnabled(Context context, boolean enabled)
    {
          Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
    }

向AndroidManifest.xml添加权限

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

您可以在此处找到文档 >

There are a lot of cool widgets out there that will enable and disable auto rotate on your phone. Disabling it turns it off across all apps on the phone.

Any ideas how they are accomplishing it?

This should do the trick for you:

    import android.provider.Settings;
    public static void setAutoOrientationEnabled(Context context, boolean enabled)
    {
          Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
    }

Add permission to the AndroidManifest.xml

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

You can find the documentation here