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

Zer*_*ero 2 android rotation button android-intent

我正在开发一个Android应用程序,我希望用户能够按下启用或禁用自动旋转的按钮.我怎么能用Intent做到这一点?我想我会以某种方式将ACCELEROMETER_ROTATION更改为0或1,但我不知道如何准确地做到这一点.我希望也许你们中的一个人可以帮助我!

ρяσ*_*я K 9

您可以使用ACCELEROMETER_ROTATION打开/关闭旋转:

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();
    }
Run Code Online (Sandbox Code Playgroud)

最后android.permission.WRITE_SETTINGS在Manifast中添加权限