[Android event processing] respond to events set by the system (Configuration class)

Posted by ironside82 on Mon, 31 Jan 2022 08:14:39 +0100

Events in response to system settings (Configuration class)

Introduction to this section

**The Configuration class introduced in this section is used to describe the Configuration information of mobile devices, such as screen direction, touch mode of touch screen, etc. I believe all friends who have customized ROM should know that we can find it in: Frameworks / base / core / Java / Android / content / RES / Configuration Java find this class, and then change the relevant settings, such as adjusting the size of the default font! If you are interested, you can understand by yourself! This section explains the use of the Configuration class in our Android development ~**

1. List of methods provided by configuration

  • densityDpi: screen density
  • fontScale: the scaling factor of the font set by the current user
  • hardKeyboardHidden: judge whether the hard keyboard is visible. There are two optional values: HARDKEYBOARDHIDDEN_NO,HARDKEYBOARDHIDDEN_YES, which are hexadecimal 0 and 1 respectively
  • Keyboard: get the current associated amount. Keyboard type: return value of this property: KEYBOARD_12KEY (keypad with only 12 keys), KEYBOARD_NOKEYS,KEYBOARD_QWERTY (ordinary keyboard)
  • keyboardHidden: this property returns a boolean value to identify whether the current keyboard is available. This attribute determines not only the hardware keyboard of the system, but also the software keyboard of the system (located on the screen).
  • Locale: get the user's current locale
  • mcc: get the country code of mobile signal
  • mnc: get the network code of mobile signal
    ps: country code and network code jointly determine the current mobile network operator
  • Navigation: judge the type of directional navigation equipment in the system. Return value of this property: NAVIGATION_NONAV (no navigation), navigation_ Dpad navigation_ Trackback, NAVIGATION_WHEEL navigation
  • Orientation: get the orientation of the system screen. Return value of this property: orientation_ Landscale, orientation_ Transportit (vertical screen)
  • Screen height dp, screen width dp: the screen can be high and wide, expressed in dp
  • Touchscreen: get the touch mode of the system touch screen. Return value of this property: touchscreen_ Notch (no touch screen), TOUCHSCREEN_STYLUS, TOUCHSCREEN_FINGER (touch screen for receiving fingers)

Back to top

2. Simple example test

Operation screenshot:

Code implementation:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView txtResult = (TextView) findViewById(R.id.txtResult);
        StringBuffer status = new StringBuffer();
        //① Gets the Configuration object of the system
        Configuration cfg = getResources().getConfiguration();
        //② Check what you want
        status.append("densityDpi:" + cfg.densityDpi + "\n");
        status.append("fontScale:" + cfg.fontScale + "\n");
        status.append("hardKeyboardHidden:" + cfg.hardKeyboardHidden + "\n");
        status.append("keyboard:" + cfg.keyboard + "\n");
        status.append("keyboardHidden:" + cfg.keyboardHidden + "\n");
        status.append("locale:" + cfg.locale + "\n");
        status.append("mcc:" + cfg.mcc + "\n");
        status.append("mnc:" + cfg.mnc + "\n");
        status.append("navigation:" + cfg.navigation + "\n");
        status.append("navigationHidden:" + cfg.navigationHidden + "\n");
        status.append("orientation:" + cfg.orientation + "\n");
        status.append("screenHeightDp:" + cfg.screenHeightDp + "\n");
        status.append("screenWidthDp:" + cfg.screenWidthDp + "\n");
        status.append("screenLayout:" + cfg.screenLayout + "\n");
        status.append("smallestScreenWidthDp:" + cfg.densityDpi + "\n");
        status.append("touchscreen:" + cfg.densityDpi + "\n");
        status.append("uiMode:" + cfg.densityDpi + "\n");
        txtResult.setText(status.toString());
    }
}

Back to top

3. Rewrite onConfigurationChanged to respond to system setting changes

This method is used to monitor the change of system settings. It is a time processing method based on callback. It will be triggered automatically when the system settings change; However, it should be noted that if the following method is used for monitoring, the targetSdkVersion attribute can only be set to 12 at most. If it is higher than 12, the method will not be fired! Here is an example of horizontal and vertical screen switching for your reference. Others can Google by themselves~

Code example: a simple button, click to switch the horizontal and vertical screen, and then Toast prompt

Operation effect diagram:

Implementation code:

public class MainActivity extends Activity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        Button btn = (Button) findViewById(R.id.btncahange);  
        btn.setOnClickListener(new OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
                Configuration config = getResources().getConfiguration();  
                //If it is horizontal screen, switch to vertical screen  
                if(config.orientation == Configuration.ORIENTATION_LANDSCAPE)  
                {  
                    MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
                }  
                //Switch to horizontal screen if vertical screen  
                if(config.orientation == Configuration.ORIENTATION_PORTRAIT)  
                {  
                    MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
                }  
            }  
        });  
    }  
      
    @Override  
    public void onConfigurationChanged(Configuration newConfig) {  
        super.onConfigurationChanged(newConfig);  
        String screen = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE?"Horizontal screen":"Vertical screen";  
        Toast.makeText(MainActivity.this, "The system screen orientation has changed \n The modified direction is" + screen, Toast.LENGTH_SHORT).show();  
    }  
}  

In addition, you need to use the Android manifest XML add the following:

Permission: < uses permission Android: name = "android.permission.CHANGE_CONFIGURATION" / >

Add: android:configChanges = "orientation" in < activity tag, and change targetSdkVersion to above 12, 12 is also OK

Back to top

Topics: Android