Android: Toggle Accelerometer Rotation State Setting

accelerometer, android, default, rotation, settings

Solution

Have you set the correct permission?

I didn't and the exception detail message says,

`"Permission Denial: writing com.android.providers.settings.SettingsProvider uri content://settings/system from pid=306, uid=10037 requires android.permission.WRITE_SETTINGS"`

That pretty much sums it up.

Problem

I've found load of examples of setting rotation within your application but what I'm having trouble with is toggling the phones setting located at: Settings, Display, Auto-rotate screen (checkbox). I want to be able to check the phones rotation state, set it to it's opposite (auto or off) and then close the app. This is what I've got: ``` import android.app.Activity; import android.os.Bundle; import android.provider.Settings; import android.widget.Toast; public class Rotation extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); try{ 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(); finish(); } else{ android.provider.Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1); Toast.makeText(Rotation.this, "Rotation ON", Toast.LENGTH_SHORT).show(); finish(); } } catch(Exception e){ e.printStackTrace(); Toast.makeText(Rotation.this, "Try failed!", Toast.LENGTH_SHORT).show(); finish(); } } } ``` if you need to know any more info just let me know :) Any help is appreciated, it's stressing me out!!

Original source