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代码:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <LinearLayout
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. xmlns:android="http://schemas.android.com/apk/res/android">
  7. <EditText
  8. android:id="@+id/conori"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. />
  12. <EditText
  13. android:id="@+id/connavigation"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. />
  17. <EditText
  18. android:id="@+id/contouch"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. />
  22. <EditText
  23. android:id="@+id/conmnc"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. />
  27. <Button
  28. android:id="@+id/conbu"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:layout_marginLeft="70dp"
  32. android:text="获取手机信息"
  33. />
  34. </LinearLayout>
Java代码:

[html] view plain copy
  1. packagecom.demo.configuration;
  2. importcom.example.demo.R;
  3. importandroid.app.Activity;
  4. importandroid.content.res.Configuration;
  5. importandroid.os.Bundle;
  6. importandroid.view.View;
  7. importandroid.view.View.OnClickListener;
  8. importandroid.widget.Button;
  9. importandroid.widget.EditText;
  10. publicclassconfigurationTestextendsActivity{
  11. privateEditTextori;
  12. privateEditTextnavigation;
  13. privateEditTexttouch;
  14. privateEditTextmnc;
  15. privateButtonbn;
  16. @Override
  17. protectedvoidonCreate(BundlesavedInstanceState){
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.configurationtest);
  20. //获取应用界面中的界面组件
  21. ori=(EditText)findViewById(R.id.conori);
  22. navigation=(EditText)findViewById(R.id.connavigation);
  23. touch=(EditText)findViewById(R.id.contouch);
  24. mnc=(EditText)findViewById(R.id.conmnc);
  25. bn=(Button)findViewById(R.id.conbu);
  26. bn.setOnClickListener(newOnClickListener(){
  27. @Override
  28. publicvoidonClick(Viewv){
  29. //TODOAuto-generatedmethodstub
  30. //获取系统的Configuration对象
  31. Configurationcfg=getResources().getConfiguration();
  32. Stringscreen=cfg.orientation==Configuration.ORIENTATION_LANDSCAPE?"横向屏幕":"竖向屏幕";
  33. ori.setText(screen);
  34. StringmncCode=cfg.mcc+"";
  35. mnc.setText(mncCode);
  36. StringnaviName=cfg.orientation==Configuration.NAVIGATION_NONAV
  37. ?"没有方向控制":cfg.orientation==Configuration.NAVIGATION_WHEEL
  38. ?"滚轮方向控制":cfg.orientation==Configuration.NAVIGATION_DPAD
  39. ?"方向键控制方向":"轨迹球控制方向";
  40. navigation.setText(naviName);
  41. StringtouchName=cfg.touchscreen==Configuration.TOUCHSCREEN_NOTOUCH
  42. ?"无触摸屏":cfg.touchscreen==Configuration.TOUCHSCREEN_STYLUS
  43. ?"触摸笔式触摸屏":"接收手指的触摸屏";
  44. touch.setText(touchName);
  45. }});
  46. }
  47. }

如果程序需要监听系统设置的更改,可以考虑重写Activity的onConfigurationChanged方法,该方法时一个基于回调的事件处理方法.

下面案例:点击按钮改变系统屏幕的方向

[html] view plain copy
  1. publicclasschangeCfgextendsActivity{
  2. privateButtonbu1;
  3. @Override
  4. protectedvoidonCreate(BundlesavedInstanceState){
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.changcfg);
  7. bu1=(Button)findViewById(R.id.changcfgbu);
  8. bu1.setOnClickListener(newOnClickListener(){
  9. @Override
  10. publicvoidonClick(Viewv){
  11. //TODOAuto-generatedmethodstub
  12. Configurationconfig=getResources().getConfiguration();
  13. //如果当前为横屏
  14. if(config.orientation==Configuration.ORIENTATION_LANDSCAPE){
  15. //设置为竖屏
  16. changeCfg.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  17. }
  18. //如果当前为竖屏
  19. if(config.orientation==Configuration.ORIENTATION_PORTRAIT){
  20. //设置为横屏
  21. changeCfg.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  22. }
  23. }
  24. });
  25. }
  26. //重写该方法,用于监听系统设置的更改
  27. @Override
  28. publicvoidonConfigurationChanged(ConfigurationnewConfig){
  29. super.onConfigurationChanged(newConfig);
  30. Stringscreen=newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE?"横屏":"竖屏";
  31. Toast.makeText(this,screen,Toast.LENGTH_LONG).show();
  32. }

更多相关文章

  1. android MediaRecorder实现录屏时带录音功能
  2. Android获取app应用程序大小的方法
  3. Android保存图片到系统相册并更新
  4. 部分 CM11 系统 Android(安卓)平板运行植物大战僵尸 2 黑屏的解
  5. Android的存储系统—Vold与MountService分析(三)
  6. Android应用程序的获取,安装。
  7. Android系统中的.apk文件和dex文件
  8. Android如何获取手机各项信息
  9. android httpclient 302自动跳转

随机推荐

  1. android在ubuntu桌面系统下编译可能的错
  2. 解决安卓webview不支持input type=file问
  3. 基于XMPP实现的Openfire的配置安装+Andro
  4. Android的各种分辨率
  5. Android文档学习05_网络1
  6. android 兼容性测试
  7. Android(安卓)Debug Bridge(ADB)的配置
  8. Android(安卓)开始支持Vulkan图形编程接
  9. 安卓版本问题compileSdkVersion, minSdkV
  10. Android通过ImageView设置手指滑动控件缩