Android(安卓)Configuration系统设置
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
- <?xmlversion="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>
[html] view plain copy
- packagecom.demo.configuration;
- importcom.example.demo.R;
- importandroid.app.Activity;
- importandroid.content.res.Configuration;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- importandroid.widget.EditText;
- publicclassconfigurationTestextendsActivity{
- privateEditTextori;
- privateEditTextnavigation;
- privateEditTexttouch;
- privateEditTextmnc;
- privateButtonbn;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- 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(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- //获取系统的Configuration对象
- Configurationcfg=getResources().getConfiguration();
- Stringscreen=cfg.orientation==Configuration.ORIENTATION_LANDSCAPE?"横向屏幕":"竖向屏幕";
- ori.setText(screen);
- StringmncCode=cfg.mcc+"";
- mnc.setText(mncCode);
- StringnaviName=cfg.orientation==Configuration.NAVIGATION_NONAV
- ?"没有方向控制":cfg.orientation==Configuration.NAVIGATION_WHEEL
- ?"滚轮方向控制":cfg.orientation==Configuration.NAVIGATION_DPAD
- ?"方向键控制方向":"轨迹球控制方向";
- navigation.setText(naviName);
- StringtouchName=cfg.touchscreen==Configuration.TOUCHSCREEN_NOTOUCH
- ?"无触摸屏":cfg.touchscreen==Configuration.TOUCHSCREEN_STYLUS
- ?"触摸笔式触摸屏":"接收手指的触摸屏";
- touch.setText(touchName);
- }});
- }
- }
如果程序需要监听系统设置的更改,可以考虑重写Activity的onConfigurationChanged方法,该方法时一个基于回调的事件处理方法.
下面案例:点击按钮改变系统屏幕的方向
[html] view plain copy
- publicclasschangeCfgextendsActivity{
- privateButtonbu1;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.changcfg);
- bu1=(Button)findViewById(R.id.changcfgbu);
- bu1.setOnClickListener(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- Configurationconfig=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
- publicvoidonConfigurationChanged(ConfigurationnewConfig){
- super.onConfigurationChanged(newConfig);
- Stringscreen=newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE?"横屏":"竖屏";
- Toast.makeText(this,screen,Toast.LENGTH_LONG).show();
- }
更多相关文章
- android MediaRecorder实现录屏时带录音功能
- Android获取app应用程序大小的方法
- Android保存图片到系统相册并更新
- 部分 CM11 系统 Android(安卓)平板运行植物大战僵尸 2 黑屏的解
- Android的存储系统—Vold与MountService分析(三)
- Android应用程序的获取,安装。
- Android系统中的.apk文件和dex文件
- Android如何获取手机各项信息
- android httpclient 302自动跳转