The configuration class is used to obtain information about the device on the phone, including user Configuration items and system dynamic settings.
The following methods of activity can be obtained:
Configuration cfg = getresource (). GetConfiguration ();
Property
public float Fontscale; Gets the zoom factor for the font currently set by the user
public int MCC; Get the country code for the mobile signal
public int MNC; Get the network code for the mobile signal
Public locale locale; Gets the user's current local
Public Boolean usersetlocale;//locale should persist on setting. This is hidden because it's really * questionable whether this is the right-and-right-to-expose the functionality.
public int Keyboardhidden: Gets the keyboard type that is associated with the current device.
Keyboard_nokeys, Keyboard_qwerty (ordinary computer keyboard),
Keyboard_12key (keypad with 12 keys only)
public int Keyboardhidden; This property returns a flag whether the current keyboard is available: (this property will not only determine the system's hardware keyboard, but also determine the system's software keyboard (on the screen))
1. If the system's hardware keyboard is unavailable, but the software keyboard is available, it returns KEYBOARDHIDDEN_NO (),
2. Return Keyboardhodden_yes only if two keyboards are not available
public int Orientation: Gets the orientation of the system screen: orientation_landspace (horizontal screen), orientation_portrait (vertical screen), Orientation_ Squares (square screen) and other properties
Now let's implement a simple screen switch (by clicking on a normal button):
Main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context = "${relativepackage}.${activityclass}" > <button android:id= "@+id/button1" android:layout_width= "WR Ap_content "android:layout_height=" Wrap_content "android:layout_centerhorizontal=" true "android:layou t_margintop= "142DP" android:text= "Click to change the screen orientation"/></relativelayout>
Main.java
public class main extends activity {@Overrideprotected void oncreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); button b = (Button) findviewbyid (R.id.button1); B.setonclicklistener (new Onclicklistener () {@Overridepublic void onclick (view v) {Configuration cfg = getresources (). GetConfiguration ();if (cfg.orientation == configuration.orientation_ LANDSCAPE) {main.this.setrequestedorientation (activityinfo.screen_orientation_portrait);} if (cfg.orientation == configuration.orientation_portrait) { Main.this.setRequestedOrientation (Activityinfo.screen_orientation_landscape);}});} /** * setting * android:configchanges= "Orientation" in the Androidmanifest.xml file * Screen orientation changes, this method will be recalled */@Overridepublic void onconfigurationchanged (configuration Newconfig) {super.onconfigurationchanged (Newconfig); string str = newconfig.orientation == configuration.orientation_landscape ? "Horizontal screen display": "vertical screen display"; Toast.maketext (This, str, toast.length_long). Show ();}}
Androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http// Schemas.android.com/apk/res/android " package=" Com.example.main " Android:versioncode= "1" android:versionname= "1.0" > < Uses-sdk android:minsdkversion= "Ten" android:targetsdkversion= " /> <application " android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@ String/app_name " android:theme=" @style/apptheme " > <!-- Set activity to monitor changes in screen orientation --> <activity android:name= ". Main " android:label=" @string/app_name " android:configchanges= "Orientation" > <intent-filter> <action Android:name= "Android.intent.action.MAIN" /> <category android:name= "Android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
!!! in the configuration file:
Android:targetsdkversion= "12"
Should be 12, set too high, the onconfigurationchanged () method will not be triggered.
Android enables simple switching between portrait and screen