Respond to system-set events (Configuration class)


Introduction to this section:

The Configuration class introduced in this section is used to describe the configuration information of mobile phone devices, such as screen orientation. The touch method of the touch screen, etc. I believe friends who have customized ROM should know that we can: frameworks/base/core/java/android/content/res/Configuration.java Find this class, and then change the relevant settings, such as adjusting the default font size! If you are interested, you can find out on your own! The use of the Configuration class explained in this section in our Android development~ API document: Configuration


1.Configuration provides us with the method list

  • ##densityDpi: Screen density
  • fontScale: The scaling factor of the font set by the current user
  • hardKeyboardHidden: Determines whether the hard keyboard is visible. There are two optional values: HARDKEYBOARDHIDDEN_NO, HARDKEYBOARDHIDDEN_YES, They are hexadecimal 0 and 1
  • keyboard: Get the current associated keyboard type: the return value of this attribute: KEYBOARD_12KEY (a small keyboard with only 12 keys), KEYBOARD_NOKEYS, KEYBOARD_QWERTY (normal keyboard)
  • keyboardHidden: This property returns a boolean value to identify whether the current keyboard is available. This property not only determines the system's hardware keyboard, but also determines the system's soft keyboard (located on the screen).
  • locale: Get the user’s current locale
  • mcc: Get the country code of the mobile signal
  • mnc : Get the network code of mobile signal ps: The country code and network code jointly determine the current mobile phone network operator
  • navigation: Determine the type of navigation device on the system. The return value of this attribute: NAVIGATION_NONAV (no navigation), NAVIGATION_DPAD (DPAD navigation) NAVIGATION_TRACKBALL (trackball navigation), NAVIGATION_WHEEL (wheel navigation)
  • orientation: Get the direction of the system screen. The return value of this attribute: ORIENTATION_LANDSCAPE (horizontal screen), ORIENTATION_PORTRAIT (vertical screen)
  • screenHeightDp, screenWidthDp: The available height and width of the screen are represented by dp
  • touchscreen: Get the touch mode of the system touch screen. The return value of this attribute: TOUCHSCREEN_NOTOUCH (no touch screen), TOUCHSCREEN_STYLUS (stylus touch screen), TOUCHSCREEN_FINGER (touch screen that receives fingers)

2. Write a simple example to test :

Running screenshot:

1.png

## 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();
        //①获取系统的Configuration对象
        Configuration cfg = getResources().getConfiguration();
        //②想查什么查什么
        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());
    }
}

3. Rewrite onConfigurationChanged to respond to changes in system settings

This method is used to monitor changes in system settings. It is a time processing method based on callbacks. When the system settings change, will be triggered automatically; But please note that if you use the following method to monitor, the targetSdkVersion attribute can only be set to a maximum of 12. If it is higher than 12, the method will not be triggered! Here is an example of switching between horizontal and vertical screens for your reference. For other information, you can Google it by yourself~

Code example: A simple button, click to switch between horizontal and vertical screens, and then Toast Tip

Running renderings:

2.jpg3.jpg

##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(config.orientation == Configuration.ORIENTATION_LANDSCAPE)  
                {  
                    MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
                }  
                //如果竖屏的话切换成横屏  
                if(config.orientation == Configuration.ORIENTATION_PORTRAIT)  
                {  
                    MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
                }  
            }  
});
          
    
                                                                                                                                                                                                                   ​ fig.orientation == Configuration.ORIENTATION_LANDSCAPE ?"Horizontal screen":"Vertical screen";
Toast.makeText(MainActivity.this, "The system screen direction has changed\nThe modified direction is" + screen, Toast.LENGTH_SHORT).show();
}  
}


In addition, you need to add the following content to AndroidManifest.xml:

Permission:

< uses-permission android:name ="android.permission.CHANGE_CONFIGURATION" />
Add in the < activity tag:

android:configChanges="orientation"Change the targetSdkVersion to 12 or above, 12 is also acceptable

Summary of this section:

This section explains to you: Configuration class and onConfigurationChanged respond to system setting changes. You can have a rough understanding. We will use it later and continue to delve deeper~