Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Toggle Accelerometer Rotation State Setting

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!!

like image 976
ask.luke Avatar asked Apr 05 '12 22:04

ask.luke


1 Answers

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.

like image 134
gnichola Avatar answered Nov 11 '22 12:11

gnichola