简体   繁体   English

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

[英]Set Auto-Rotate Enabled/Disabled Android

I am working on an Android app, for which I would like the user to be able to press a button which either enables or disables auto-rotate. 我正在开发一个Android应用程序,我希望用户能够按下启用或禁用自动旋转的按钮。 How can I do this with an Intent? 我怎么能用Intent做到这一点? I'd imagine I would somehow need to change ACCELEROMETER_ROTATION to 0 or 1, but I don't know how to do this precisely. 我想我会以某种方式将ACCELEROMETER_ROTATION更改为0或1,但我不知道如何准确地做到这一点。 I hope maybe one of you guys can help me out! 我希望也许你们中的一个人可以帮助我!

You can toggle Rotation ON/OFF using ACCELEROMETER_ROTATION as : 您可以使用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();
    }

and finally add android.permission.WRITE_SETTINGS permission in Manifast 最后在Manifast中添加android.permission.WRITE_SETTINGS权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM