Configuration类是专门用来描述手机设备上的配置信息。这些配置信息包括用户特定的配置项,也包括系统的动态设备配置。
程序中可调用Activity的如下方法来获取Configuration对象
//获取系统的Configuration对象
Configuration cfg = getResources().getConfiguration();
其中以下的参数代表的配置信息
fontScale:获取当前用户设置的字体的缩放因子。
keyboard:获取当前设备所关联的键盘类型。该属性的返回值:KEYBOARD_12KEY(只有12个键的小键盘)、KEYBOARD_NOKEYS、KEYBOARD_QWERTY(普通键盘)
keyboardHidden:该属性返回一个boolean值用于标识当前键盘是否可用。该属性不仅会判断系统的硬件键盘,也会判断系统的软键盘(位于屏幕)。
locale:获取用户当前的Locale.
mcc:获取移动信号的国家码
mnc:获取移动信号的网络码
navigation:判断系统上方向导航设备的类型。该属性的返回值:NAVIGATION_NONAV(无导航)、NAVIGATION_DPAD(DPAD导航)
、NAVIGATION_TRACKBALL(轨迹球导航)、NAVIGATION_WHEEL(滚轮导航)
orientation:获取系统屏幕的方向。该属性的返回值:ORIENTATION_LANDSCAPE(横向屏幕)、ORIENTATION_PORTRAIT(竖向屏幕)
touchscreen:获取系统触摸屏的触摸方式。该属性的返回值:TOUCHSCREEN_NOTOUCH(无触摸屏)、TOUCHSCREEN_STYLUS(触摸笔式触摸屏)、
TOUCHSCREEN_FINGER(接收手指的触摸屏)
案例:获取手机系统的设备状态
XML代码:
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <EditText
- android:id="@+id/conori"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- />
- <EditText
- android:id="@+id/connavigation"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- />
- <EditText
- android:id="@+id/contouch"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- />
- <EditText
- android:id="@+id/conmnc"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/conbu"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="70dp"
- android:text="获取手机信息"
- />
- </LinearLayout>
- package com.demo.configuration;
- import com.example.demo.R;
- import android.app.Activity;
- import android.content.res.Configuration;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class configurationTest extends Activity{
- private EditText ori;
- private EditText navigation;
- private EditText touch;
- private EditText mnc;
- private Button bn;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.configurationtest);
- //获取应用界面中的界面组件
- ori = (EditText)findViewById(R.id.conori);
- navigation = (EditText)findViewById(R.id.connavigation);
- touch = (EditText)findViewById(R.id.contouch);
- mnc = (EditText)findViewById(R.id.conmnc);
- bn = (Button)findViewById(R.id.conbu);
- bn.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- //获取系统的Configuration对象
- Configuration cfg = getResources().getConfiguration();
- String screen = cfg.orientation == Configuration.ORIENTATION_LANDSCAPE ? "横向屏幕" : "竖向屏幕";
- ori.setText(screen);
- String mncCode = cfg.mcc + "";
- mnc.setText(mncCode);
- String naviName = cfg.orientation == Configuration.NAVIGATION_NONAV
- ? "没有方向控制" : cfg.orientation == Configuration.NAVIGATION_WHEEL
- ? "滚轮方向控制" : cfg.orientation == Configuration.NAVIGATION_DPAD
- ? "方向键控制方向" : "轨迹球控制方向";
- navigation.setText(naviName);
- String touchName = cfg.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH
- ? "无触摸屏" : cfg.touchscreen == Configuration.TOUCHSCREEN_STYLUS
- ? "触摸笔式触摸屏" : "接收手指的触摸屏";
- touch.setText(touchName);
- }});
- }
- }
如果程序需要监听系统设置的更改,可以考虑重写Activity的onConfigurationChanged方法,该方法时一个基于回调的事件处理方法.
下面案例:点击按钮改变系统屏幕的方向
- public class changeCfg extends Activity{
- private Button bu1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.changcfg);
- bu1 = (Button)findViewById(R.id.changcfgbu);
- bu1.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Configuration config = getResources().getConfiguration();
- //如果当前为横屏
- if(config.orientation == Configuration.ORIENTATION_LANDSCAPE){
- //设置为竖屏
- changeCfg.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
- }
- //如果当前为竖屏
- if(config.orientation == Configuration.ORIENTATION_PORTRAIT){
- //设置为横屏
- changeCfg.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
- }
- }
- });
- }
- //重写该方法,用于监听系统设置的更改
- @Override
- public void onConfigurationChanged(Configuration newConfig){
- super.onConfigurationChanged(newConfig);
- String screen = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ?"横屏":"竖屏";
- Toast.makeText(this, screen, Toast.LENGTH_LONG).show();
- }