设置自动旋转启用/禁用Android

设置自动旋转启用/禁用Android,android,button,android-intent,rotation,Android,Button,Android Intent,Rotation,我正在开发一个Android应用程序,我希望用户能够按下一个按钮来启用或禁用自动旋转。我怎样才能有目的地做到这一点?我想我可能需要将加速度计的旋转改为0或1,但我不知道如何精确地做到这一点。我希望你们中的一个能帮我 您可以使用加速度计旋转开关打开/关闭旋转,如下所示: if (android.provider.Settings.System.getInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0) == 1){

我正在开发一个Android应用程序,我希望用户能够按下一个按钮来启用或禁用自动旋转。我怎样才能有目的地做到这一点?我想我可能需要将加速度计的旋转改为0或1,但我不知道如何精确地做到这一点。我希望你们中的一个能帮我

您可以使用加速度计旋转开关打开/关闭旋转,如下所示:

if  (android.provider.Settings.System.getInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0) == 1){
    android.provider.Settings.System.putInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0);
    Toast.makeText(Rotation.this, "Rotation OFF", Toast.LENGTH_SHORT).show();
    }
else{
    android.provider.Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
    Toast.makeText(Rotation.this, "Rotation ON", Toast.LENGTH_SHORT).show();
    }

最后添加
android.permission.WRITE_SETTINGS
permission in Manifast

非常感谢您的回复,但是我如何使用它呢?我不能在我的应用程序中使用onClick.intent意味着告诉更多你想要的注意:从API 23(Android M-6.x)开始,你需要请求许可,如下所示: