简体   繁体   中英

Enable and disable auto rotate programmatically?

There are a lot of cool widgets out there that will enable and disable auto rotate on your phone. Disabling it turns it off across all apps on the phone.

Any ideas how they are accomplishing it?

This should do the trick for you:

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

Add permission to the AndroidManifest.xml

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

You can find the documentation here

始终使用用户指定的屏幕方向,这将应用用户选择的任何方向,并且在禁用屏幕时不会旋转屏幕。

activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);

This is my impementation for this problem. I had to implement a button which had the same function that the lockbutton from the settings menu.

you can use the setRotationScreenFromSettings to solve your issue

public static boolean getRotationScreenFromSettingsIsEnabled(Context context)
{

    int result = 0;
    try
    {
        result = Settings.System.getInt(context.getContentResolver(), 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.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION) == 1)
        {
            Display defaultDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
            Settings.System.putInt(context.getContentResolver(), Settings.System.USER_ROTATION, defaultDisplay.getRotation());
            Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
        }
        else
        {
            Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
        }

        Settings.System.putInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);

    }
    catch (Settings.SettingNotFoundException e)
    {
        e.printStackTrace();
    }
}

 private void regirsterLockScreenOrientationChangedListner()
{
    ContentObserver rotationObserver = new ContentObserver(new Handler())
    {
        @Override
        public void onChange(boolean selfChange)
        {
            refreshLockScreenOrientationBtn();
        }
    };

    context.getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION),
            true, rotationObserver);
}`

Add permission to the AndroidManifest.xml

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

You have to fix it using following code inside onCreate() Function. Working...

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM