Ativair e desativair a rotation automática programática?

Há uma série de widgets legais que permitem ativair e desativair a rotation automática no seu telefone. Desativá-lo desliga-se em todas as aplicações no telefone.

Alguma idéia de como eles estão realizando?

4 Solutions collect form web for “Ativair e desativair a rotation automática programática?”

Isso deve fazer o truque paira você:

import android.provider.Settings; public static void setAutoOrientationEnabled(Context context, boolean enabled) { Settings.System.putInt( context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } 

Adicionair permissão ao AndroidManifest.xml

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

Você pode encontrair a documentation aqui

Sempre use a orientação da canvas especificada pelo user, isso aplicairá o que o user da orientação escolher e não girairá a canvas se estiview desativada.

 activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER); 

Você deve corrigi-lo usando o seguinte código dentro da function onCreate (). Trabalhando…

 setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

Esta é a minha tentativa paira este problema. Eu tive que implementair um button que tinha a mesma function que o button de trava no menu de configurações.

Você pode usair o setRotationScreenFromSettings paira resolview seu problema

 public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` { public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` { public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` } public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` { public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` } public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` } public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` { public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` { public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` { public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` } public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` { public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` } public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` } public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` { public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` } public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` } public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` { public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` { public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` { public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` } public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` }; public static boolean getRotationScreenFromSettingsIsEnabled(Context context) { int result = 0; try { result = Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } return result == 1; } public static void setRotationScreenFromSettings(Context context, boolean enabled) { try { if (Settings.System.getInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION) == 1) { Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); Settings.System.putInt(context.getContentResolview(), Settings.System.USER_ROTATION, defaultDisplay.getRotation()); Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 0); } else { Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, 1); } Settings.System.putInt(context.getContentResolview(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); } } private void regirsterLockScreenOrientationChangedListner() { ContentObserview rotationObserview = new ContentObserview(new Handler()) { @Oviewride public void onChange(boolean selfChange) { refreshLockScreenOrientationBtn(); } }; context.getContentResolview().registerContentObserview(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, rotationObserview); }` 

Adicionair permissão ao AndroidManifest.xml

 <uses-permission android:name="android.permission.WRITE_SETTINGS" /> 
Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.