Android monitoring horizontal and vertical screen switching

Occasionally, when playing a video in a project, the video needs to be played in full screen in a horizontal screen, so it is necessary to monitor the horizontal and vertical screen switching events of the screen.

ConfigChanges is used to capture changes in the state of the mobile phone. When the horizontal and vertical screens are switched, the screen size changes, the keyboard pops up, the system settings change and other conditions, the callback event onConfigurationChanged will be triggered. Let the Activity capture the event, you need to do the following steps:

  1. Declare the type of event that the Activity needs to capture, in the manifest configuration:
        <activity android:name=".MainActivity"
            android:configChanges="orientation|keyboard|layoutDirection|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

configChanges configuration parameter description:

"mcc" mobile country code, consisting of three digits, each country has its own independent MCC, which can identify the country to which the mobile phone user belongs.
"mnc" mobile network number, used to distinguish the service provider of mobile phone users in a country or region.
The locale of "locale" has changed.
"touchscreen" The touchscreen has been changed.
"keyboard" The keyboard mode changes, for example: the user accesses an external keyboard input.
"keyboardHidden" The user opens the hardware keyboard of the mobile phone
"navigation" The navigation has changed.
"orientation" switch between horizontal and vertical screens.
"fontScale" global font size scaling changed

  1. Rewrite the onConfigurationChanged method in Activity, and add your own event processing.
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            Toast.makeText(getApplicationContext(), "横屏", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getApplicationContext(), "竖屏", Toast.LENGTH_SHORT).show();
        }
    }

Configuration.ORIENTATION_LANDSCAPE horizontal screen
Configuration.ORIENTATION_PORTRAIT vertical screen

in addition:

  • When the android:configChanges attribute of the activity is not set, each life cycle method will be called when switching between horizontal and vertical screens, once for horizontal screens, and twice for vertical screens
  • When setting the android:configChanges="orientation" attribute of the activity, each life cycle method will be called when switching between horizontal and vertical screens, once for horizontal screens, and once for vertical screens
  • When setting the activity's android:configChanges="orientation|keyboardHidden", switching between horizontal and vertical screens will not call each life cycle method, but will only execute the onConfigurationChanged method

Guess you like

Origin blog.csdn.net/xige1995/article/details/124092186