Configuration類是專門用來描述手機設備上的配置信息。這些配置信息包括用戶特定的配置項,也包括系統的動態設備配置。html
程序中可調用Activity的以下方法來獲取Configuration對象android
//獲取系統的Configuration對象
Configuration cfg = getResources().getConfiguration();
網絡
其中如下的參數表明的配置信息app
fontScale:獲取當前用戶設置的字體的縮放因子。ide
keyboard:獲取當前設備所關聯的鍵盤類型。該屬性的返回值:KEYBOARD_12KEY(只有12個鍵的小鍵盤)、KEYBOARD_NOKEYS、KEYBOARD_QWERTY(普通鍵盤)字體
keyboardHidden:該屬性返回一個boolean值用於標識當前鍵盤是否可用。該屬性不只會判斷系統的硬件鍵盤,也會判斷系統的軟鍵盤(位於屏幕)。this
locale:獲取用戶當前的Locale.spa
mcc:獲取移動信號的國家碼.net
mnc:獲取移動信號的網絡碼code
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 plaincopy
<?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>
Java代碼:
[html] view plaincopy
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方法,該方法時一個基於回調的事件處理方法.
下面案例:點擊按鈕改變系統屏幕的方向
[html] view plaincopy
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();
}