Configuration类响应的系统设置的事件
Configuration类用于描述手机设备上的配置信息。
通过调用Activity的如下方法来获取系统的Configuration对象。
Configuration cfg = getResources().getConfiguration();
该对象提供了如下常用属性来获取系统的配置信息。
public float fontScale:获取当前用户设置的字体的缩放因子。
public int keyboard:获取当前设备所关联的键盘类型。该属性可能返回如下值:
KEYBOARD_NOKEYS、KEYBOARD_QWERTY普通电脑键盘、KEYBOARD_12KEY(只有12个建的小键盘)。
public int keyboardHidden:该属性返回一个boolean值用于标识当前键盘是否可用。该属性不仅会判断系统地硬件键盘,也会判断系统的软键盘(位于屏幕上)。如果该系统的硬件键盘不可用,但软键盘可用,该属性也会返回KEYBOARDHIDDEN_NO,只有当两个键盘都不可用时才会返回KEYBOARDHIDDEN_YES.
public Locale locale:获取用户当前的Locale。
public int mcc:获取移动信号的国家码。
public int mnc:获取移动信号的网络码。
public int navigation:判断系统上方向导航设备的类型。该属性可能返回如NAVIGATION_NONAY(无导航)、NAVIGATION_DPAD(DPAD导航)、NAVIGATION_TRACKBALL(轨迹球导航)、NAVIGATION_WHEEL(滚轮导航)等属性值。
public int orientation:获取系统屏幕的方向,该属性可能返回ORIENTATION_LANDSCAPE(横向屏幕)、ORIENTATION_PORTRAIT(竖向屏幕)、ORIENTATION_SQUARE(方形屏幕)等属性值。
public int touchscreen:获取系统触摸屏的触摸方式。该属性可能返回TOUCHSC_REEN_NOTOUCH(无触摸屏)、TOUCHSCREEN_STYLUS(触摸笔式的触摸屏)、TOUCHSCREEN_FINGER(接受手指的触摸屏)。
实例:获取系统设备的状态
MainActivity.java
packagecom.example.configurationtest;publicclassMainActivityextendsActivity{EditTextori;EditTextnavigation;EditTexttouch;EditTextmnc;Buttonbn;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取应用界面中的界面组件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(newOnClickListener(){//为按钮绑定事件监听器publicvoidonClick(Viewv){//获取系统的Configuration对象Configurationcfg=getResources().getConfiguration();Stringscreen=cfg.orientation==Configuration.ORIENTATION_LANDSCAPE?"横向屏幕":"竖向屏幕";StringmncCode=cfg.mnc+"";StringnaviName=cfg.orientation==Configuration.NAVIGATION_NONAV?"没有方向控制":cfg.orientation==Configuration.NAVIGATION_WHEEL?"滚轮方向控制":cfg.orientation==Configuration.NAVIGATION_DPAD?"方向键控制方向":"轨迹球控制方向";StringtouchName=cfg.touchscreen==Configuration.TOUCHSCREEN_NOTOUCH?"无触摸屏":"支持触摸屏";ori.setText(screen);mnc.setText(mncCode);navigation.setText(naviName);touch.setText(touchName);}});}}
activity_main.xml
<LinearLayoutxmlns: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"><EditTextandroid:id="@+id/ori"android:layout_width="match_parent"android:layout_height="wrap_content"/><EditTextandroid:id="@+id/navigation"android:layout_width="match_parent"android:layout_height="wrap_content"/><EditTextandroid:id="@+id/touch"android:layout_width="match_parent"android:layout_height="wrap_content"/><EditTextandroid:id="@+id/mnc"android:layout_width="match_parent"android:layout_height="wrap_content"/><Buttonandroid:id="@+id/bn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="获取系统设备状态"/></LinearLayout>
如果系统需要监听系统设置的更改,则可以考虑重写Activity的 onConfigurationChanged(Configuration newConfig)方法,该方法是一个基于回调的事件处理方法:当系统设置发生改变时,该方法会被自动触发。
实例:重写onConfigurationChanged响应系统设置更改
MainActivity.java
packagecom.example.changecfg;publicclassMainActivityextendsActivity{Buttonbn;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bn=(Button)findViewById(R.id.bn);bn.setOnClickListener(newOnClickListener(){//为按钮绑定事件监听器publicvoidonClick(Viewv){//获取系统的Configuration对象Configurationconfig=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);}}});}//重写该方法,用于监听系统设置的更改,主要是监控屏幕方向的更改publicvoidonConfigurationChanged(ConfigurationnewConfig){super.onConfigurationChanged(newConfig);Stringscreen=newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE?"横向屏幕":"竖向屏幕";Toast.makeText(this,"\n修改后的屏幕方向为"+screen,1).show();}}
activity_main.xml
<LinearLayoutxmlns: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"><Buttonandroid:id="@+id/bn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="更改屏幕方向"/></LinearLayout>
为了让Activity能监听屏幕方向的更改的时间,需要在配置改Activity时指定acdroid:configChanges属性,该属性支持的其中一种属性值为orientation,指定Activity可以监听屏幕方向改变的事件。
android:targetSdkVersion="12"最高只能设为12,不然无法监听系统设置的更改。
AndroidManifest.xml
<?xmlversion="1.0"encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.example.changecfg"android:versionCode="1"android:versionName="1.0"><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="12"/><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme"><activityandroid:name="com.example.changecfg.MainActivity"android:configChanges="orientation"android:label="@string/app_name"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity></application></manifest>
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。