Enable and disable auto rotate programmatically?

后端 未结 4 1593
孤城傲影
孤城傲影 2020-11-28 08:26

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

相关标签:
4条回答
  • 2020-11-28 08:39

    Always use user specified screen orientation, this will apply whatever orientation user has choose and will not rotate screen if its disabled.

    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
    
    0 讨论(0)
  • 2020-11-28 08:42

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

    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
    0 讨论(0)
  • 2020-11-28 09:00

    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

    0 讨论(0)
  • 2020-11-28 09:00

    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" />
    
    0 讨论(0)
提交回复
热议问题