dongshengcn dongshengcn - 3 months ago 18
Android Question

How to check if the rotation is locked on Android

I have an app, and I want my app to respect the rotation honer. But how to check if the rotation is locked on Android? Programmatically of course :)

=================

Thanks imran for answer. I put it here again because I can not find anything about DEFAULT_ACCELEROMETER_ROTATION, and also there is not need to change user-permission if you just want to read a system setting. Cheers!

http://developer.android.com/reference/android/provider/Settings.System.html#getInt(android.content.ContentResolver, java.lang.String)

int str = Settings.System.getInt(Context.getContentResolver(),

Settings.System.ACCELEROMETER_ROTATION, DEFAULT_ACCELEROMETER_ROTATION);

Answer

You can check rotation if locked or not by using System Setting as:

   int str = Settings.System.getInt(context.getContentResolver(),                                    
             Settings.System.ACCELEROMETER_ROTATION, 
             DEFAULT_ACCELEROMETER_ROTATION); 

   if(str==1) 
   {
      // rotation is Unlocked
   } else {
      // rotation is Locked
   }

AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>