Configuration class responds to system-set events

Source: Internet
Author: User

The configuration class is used to describe the information on a mobile device.

The configuration object of the system is obtained by invoking the following method of activity.

Configuration cfg = getresources (). GetConfiguration ();


This object provides the following common properties to obtain configuration information for the system.

Public float Fontscale: Gets the zoom factor for the font that is currently set by the user.

public int keyboard: Gets the keyboard type that is associated with the current device. The property may return the following values:

Keyboard_nokeys, Keyboard_qwerty General computer keyboard, Keyboard_12key (only 12 small keyboards built).

public int Keyboardhidden: This property returns a Boolean value that identifies whether the current keyboard is available. This property will not only judge the system hardware keyboard, but also determine the system's soft keyboard (on the screen). If the system's hardware keyboard is unavailable, but the soft keyboard is available, the property also returns KEYBOARDHIDDEN_NO, which returns Keyboardhidden_yes only if two keyboards are unavailable.

Public locale locale: Gets the user's current locale.

public int MCC: Gets the country code for the mobile signal.

public int MNC: Gets the network code for the mobile signal.

public int navigation: determines the type of directional navigation device on the system. This property may return property values such as Navigation_nonay (no navigation), Navigation_dpad (DPAD navigation), Navigation_trackball (trackball navigation), Navigation_wheel (wheel navigation), and so on.

public int Orientation: Gets the orientation of the system screen, which may return Orientation_landscape (Landscape screen), orientation_portrait (vertical screen), Orientation_ Squares (square screen) and other property values.

public int touchscreen: Gets the touch mode of the system touch screen. This property may return Touchsc_reen_notouch (no touch screen), Touchscreen_stylus (Touch-pen touch screen), Touchscreen_finger (touch screen with finger).


Example: Getting the status of a system device

Mainactivity.java

Package com.example.configurationtest;public class mainactivity extends activity  {EditText ori; edittext navigation; edittext touch; edittext mnc; Button bn;protected void oncreate (bundle savedinstancestate)  {super.onCreate ( Savedinstancestate); Setcontentview (r.layout.activity_main);//  get interface components in the application interface ori =  (EditText)  findviewbyid (R.id.ori);navigation =  (EditText)  findviewbyid (r.id.navigation); touch =   (EditText)  findviewbyid (R.id.touch);mnc =  (EditText)  findviewbyid (R.ID.MNC);bn  =  (Button)  findviewbyid (r.id.bn) Bn.setonclicklistener (New onclicklistener ()  {//  Bind Event Listener Public void onclick (VIEW V)  {//  Get the configuration object of the system for the button configuration cfg  = getresources (). GetConfiguration (); string screen = cfg.orientation == configuration.orientation_landscape ?  " Landscape Screen ":   " Vertical screen "; string mnccode = cfg.mnc +  ""; string naviname = cfg.orientation == configuration.navigation_nonav ?  " No direction control ": cfg.orientation == configuration.navigation_wheel ? " roller Direction Control ":  cfg.orientation == configuration.navigation_dpad ?  "Direction key control direction":  "trajectory ball control direction"; string touchname = cfg.touchscreen == configuration.touchscreen_notouch ?  " No touchscreen ": " support touchscreen "; ori.settext (screen); Mnc.settext (Mnccode); Navigation.settext (naviname); Touch.settext ( touchname);}});}}

Activity_main.xml

<linearlayout 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"     android:orientation= "vertical"  >    <edittext        android:id= "@+id /ori "        android:layout_width=" Match_parent "         android:layout_height= "Wrap_content"  />    < Edittext        android:id= "@+id/navigation"          android:layout_width= "Match_parent"          android:layout_height= "Wrap_content"  />    <EditText         android:id= "@+id/touch"         android:layout_width= "Match_parent"          android:layout_height= "Wrap_content"  />     <edittext        android:id= "@+id/mnc"          android:layout_width= "Match_parent"          android:layout_height= "Wrap_content"  />    <Button         android:id= "@+id/bn"         android:layout_ Width= "Match_parent"         android:layout_height= "Wrap_content"          android:text= "Get System device status"  /></LinearLayout>



if the system needs to listen for changes to the system settings, consider rewriting the activity's onconfigurationchanged (Configuration newconfig) method, which is a callback-based event-handling method: When the The method is automatically triggered when the system setting changes.


Example: overriding onconfigurationchanged Response system settings Change

Mainactivity.java

Package com.example.changecfg;public class mainactivity extends activity {button  bn;protected void oncreate (bundle savedinstancestate)  {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main);bn =  (Button)  findviewbyid (R.ID.BN); Bn.setonclicklistener (New onclicklistener ()  {//  bind event Listener Public void onclick for the button (View &NBSP;V)  {//  Gets the Configuration object Configuration config = getresources () of the system. GetConfiguration ();//  If the current is a horizontal screen if  (config.orientation == configuration.orientation_landscape )  {//  Set as vertical screen MainActivity.this.setRequestedOrientation (activityinfo.screen_orientation_portrait);}   If it is currently a vertical screen if  (config.orientation == configuration.orientation_portrait)  {//  Set as horizontal screen MainActivity.this.setRequestedOrientation (Activityinfo.screen_orientation_landscape);}});}   Override this method to listen for changes in system settings, primarily to monitor changes in screen orientation Public void onconFigurationchanged (Configuration newconfig)  {super.onconfigurationchanged (newConfig); string screen = newconfig.orientation == configuration.orientation_landscape ?   "Horizontal screen":  "vertical screen"; Toast.maketext (this,  "\ n Modified screen orientation is"  + screen, 1). Show ();}}


Activity_main.xml

<linearlayout 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 "Android:orien tation= "vertical" > <button android:id= "@+id/bn" android:layout_width= "Wrap_content" Android : layout_height= "wrap_content" android:text= "change screen orientation"/></linearlayout>


In order for the activity to listen for changes in the screen orientation, you need to specify the Acdroid:configchanges property when you configure the activity, and one of the property values supported by this property is orientation. Specifies that activity can listen for events that change screen orientation.


Android:targetsdkversion= "12" can only be set to 12, otherwise the system settings changes cannot be monitored.


Androidmanifest.xml

<?xml version= "1.0"  encoding= "Utf-8"? ><manifest xmlns:android= "http// Schemas.android.com/apk/res/android "    package=" Com.example.changecfg "     android:versioncode= "1"     android:versionname= "1.0"  >     <uses-sdk        android:minsdkversion= "8"          android:targetsdkversion= " />    <application"         android:allowbackup= "true"          android:icon= "@drawable/ic_launcher"         android:label= "@string/app_name"         android:theme= "@style/apptheme"  >         <activity             android:name= "Com.example.changecfg.MainActivity "             android:configchanges= "Orientation"              Android:label= "@string/app_name"  >             <intent-filter>                 <action android:name= "Android.intent.action.MAIN"  />                 <category android:name= " Android.intent.category.LAUNCHER " />             </intent-filter>        </activity>     </application></manifest>


This article is from "If life is just like the beginning" blog, please make sure to keep this source http://9944522.blog.51cto.com/9934522/1706069

Configuration class responds to system-set events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.