Configuration类专门描述手机设备上的配置信息,这些配置信息既包括用户特定的配置项,也包括系统的动态设备配置。通过调用Activity的getResources().getConfiguration()方法获得Configuration对象,然后就可以使用下面常用属性来获取系统的配置信息:
下面通过一个简单实例来演示其中几个属性的使用,代码如下:
Activity:
- package com.lovo;
- 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 ConfigActivity extends Activity {
- private EditText editText1;
- private EditText editText2;
- private EditText editText3;
- private EditText editText4;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.config_main);
- // 获得EditText系列组件对象
- editText1 = (EditText) findViewById(R.id.config_main_editText1);
- editText2 = (EditText) findViewById(R.id.config_main_editText2);
- editText3 = (EditText) findViewById(R.id.config_main_editText3);
- editText4 = (EditText) findViewById(R.id.config_main_editText4);
- // 获取Button组件实例
- Button btn = (Button) findViewById(R.id.config_main_btn);
- btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 获得系统的Configuration对象
- Configuration config = getResources().getConfiguration();
- // 获得屏幕方向
- String screen = config.orientation == Configuration.ORIENTATION_LANDSCAPE ? "横屏"
- : "竖屏";
- editText1.setText(screen);
- // 获得移动信号的网络码
- String mncCode = config.mnc + "";
- editText2.setText(mncCode);
- // 获得系统上方向导航的设备类型
- String naviName = config.navigation == Configuration.NAVIGATION_NONAV ? "没有方向控制"
- : config.navigation == Configuration.NAVIGATION_WHEEL ? "滚轮控制方向"
- : config.navigation == Configuration.NAVIGATION_DPAD ? "方向键控制方向"
- : "轨迹球控制方向";
- editText3.setText(naviName);
- // 获得系统触摸屏的触摸方式
- String touchName = config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH ? "无触摸屏"
- : config.touchscreen == Configuration.TOUCHSCREEN_STYLUS ? "触摸笔式的触摸屏"
- : "接受手指的触摸屏";
- editText4.setText(touchName);
- }
- });
- }
- }
布局XML:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_gravity="center_horizontal"
- android:orientation="vertical" >
- <EditText
- android:id="@+id/config_main_editText1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="显示屏幕方向" />
- <EditText
- android:id="@+id/config_main_editText2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="显示移动网络代号" />
- <EditText
- android:id="@+id/config_main_editText3"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="显示手机方向控制设备" />
- <EditText
- android:id="@+id/config_main_editText4"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="显示触摸屏触摸方式" />
- <Button
- android:id="@+id/config_main_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="获取手机信息" />
- </LinearLayout>
附上图片效果:
获取手机信息前:
获取手机信息后:
本文中利用按钮动态改变屏幕方向,然后重写Activity的onConfigurationChanged方法,该方法用于监听系统设置的更改,代码如下:
Activity:
- package com.lovo;
- import android.app.Activity;
- import android.content.pm.ActivityInfo;
- 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.Toast;
- public class TestActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // 获得改变系统布局的按钮
- Button btn = (Button) findViewById(R.id.btn1);
- btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 获得系统的Configuration对象
- Configuration config = getResources().getConfiguration();
- // 如果当前是横屏
- if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
- // 设为竖屏
- TestActivity.this
- .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
- }
- // 如果当前是竖屏
- if (config.orientation == Configuration.ORIENTATION_PORTRAIT) {
- // 设为横屏
- TestActivity.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, "系统屏幕方向发生改变" + "\n修改后的屏幕方向为:" + screen,
- Toast.LENGTH_LONG).show();
- }
- }
布局XML:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:orientation="vertical" >
- <Button
- android:id="@+id/btn1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="更改屏幕方向" />
- </LinearLayout>
更改系统设置时需要在manifest声明权限:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.lovo"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="10" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <!-- android:configChanges="orientation":设置Activity允许修改屏幕方向 -->
- <activity
- android:configChanges="orientation"
- android:name=".TestActivity"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity
- android:name=".TestListViewActivity"
- android:label="@string/app_name"
- android:theme="@style/AppTheme">
- </activity>
- </application>
- <!-- 授予应用程序修改系统设置的权限 -->
- <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
- </manifest>
附上图片效果: