Set Auto-Rotate Enabled/Disabled Android

2019-04-02 07:51发布

问题:

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. How can I do this with an 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. I hope maybe one of you guys can help me out!

回答1:

You can toggle Rotation ON/OFF using ACCELEROMETER_ROTATION as :

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