Configuration類響應的系統設置的事件

    Configuration類用於描述手機設備上的配置信息。

    通過調用Activity的如下方法來獲取系統的Configuration對象。

    Configuration cfg = getResources().getConfiguration();


    該對象提供瞭如下常用屬性來獲取系統的配置信息。

    public float fontScale:獲取當前用戶設置的字體的縮放因子。

    public int keyboard:獲取當前設備所關聯的鍵盤類型。該屬性可能返回如下值:

    KEYBOARD_NOKEYS、KEYBOARD_QWERTY普通電腦鍵盤、KEYBOARD_12KEY(只有12個建的小鍵盤)。

    public int keyboardHidden:該屬性返回一個boolean值用於標識當前鍵盤是否可用。該屬性不僅會判斷系統地硬件鍵盤,也會判斷系統的軟鍵盤(位於屏幕上)。如果該系統的硬件鍵盤不可用,但軟鍵盤可用,該屬性也會返回KEYBOARDHIDDEN_NO,只有當兩個鍵盤都不可用時纔會返回KEYBOARDHIDDEN_YES.

    public Locale locale:獲取用戶當前的Locale。

    public int mcc:獲取移動信號的國家碼。

    public int mnc:獲取移動信號的網絡碼。

    public int navigation:判斷系統上方向導航設備的類型。該屬性可能返回如NAVIGATION_NONAY(無導航)、NAVIGATION_DPAD(DPAD導航)、NAVIGATION_TRACKBALL(軌跡球導航)、NAVIGATION_WHEEL(滾輪導航)等屬性值。

    public int orientation:獲取系統屏幕的方向,該屬性可能返回ORIENTATION_LANDSCAPE(橫向屏幕)、ORIENTATION_PORTRAIT(豎向屏幕)、ORIENTATION_SQUARE(方形屏幕)等屬性值。

    public int touchscreen:獲取系統觸摸屏的觸摸方式。該屬性可能返回TOUCHSC_REEN_NOTOUCH(無觸摸屏)、TOUCHSCREEN_STYLUS(觸摸筆式的觸摸屏)、TOUCHSCREEN_FINGER(接受手指的觸摸屏)。


實例:獲取系統設備的狀態

MainActivity.java

package com.example.configurationtest;

public class MainActivity extends Activity {
	EditText ori;
	EditText navigation;
	EditText touch;
	EditText mnc;
	Button bn;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// 獲取應用界面中的界面組件
		ori = (EditText) findViewById(R.id.ori);
		navigation = (EditText) findViewById(R.id.navigation);
		touch = (EditText) findViewById(R.id.touch);
		mnc = (EditText) findViewById(R.id.mnc);
		bn = (Button) findViewById(R.id.bn);
		bn.setOnClickListener(new OnClickListener() {

			// 爲按鈕綁定事件監聽器
			public void onClick(View v) {
				// 獲取系統的Configuration對象
				Configuration cfg = getResources().getConfiguration();
				String screen = cfg.orientation == Configuration.ORIENTATION_LANDSCAPE ? "橫向屏幕"
						: "豎向屏幕";
				String mncCode = cfg.mnc + "";
				String naviName = cfg.orientation == Configuration.NAVIGATION_NONAV ? "沒有方向控制"
						: cfg.orientation == Configuration.NAVIGATION_WHEEL ? "滾輪方向控制"
								: cfg.orientation == Configuration.NAVIGATION_DPAD ? "方向鍵控制方向"
										: "軌跡球控制方向";
				String touchName = cfg.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH ? "無觸摸屏"
						: "支持觸摸屏";
				ori.setText(screen);
				mnc.setText(mncCode);
				navigation.setText(naviName);
				touch.setText(touchName);
			}
		});
	}
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/ori"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/touch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/mnc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/bn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="獲取系統設備狀態" />

</LinearLayout>



    如果系統需要監聽系統設置的更改,則可以考慮重寫Activity的               onConfigurationChanged(Configuration newConfig)方法,該方法是一個基於回調的事件處理方法:當系統設置發生改變時,該方法會被自動觸發。


實例:重寫onConfigurationChanged響應系統設置更改

MainActivity.java

package com.example.changecfg;

public class MainActivity extends Activity {
	Button bn;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		bn = (Button) findViewById(R.id.bn);
		bn.setOnClickListener(new OnClickListener() {
			// 爲按鈕綁定事件監聽器
			public void onClick(View v) {
				// 獲取系統的Configuration對象
				Configuration config = getResources().getConfiguration();
				// 如果當前是橫屏
				if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
					// 設爲豎屏
					MainActivity.this
							.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
				}
				// 如果當前是豎屏
				if (config.orientation == Configuration.ORIENTATION_PORTRAIT) {
					// 設爲橫屏
					MainActivity.this
							.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
				}

			}
		});
	}

	// 重寫該方法,用於監聽系統設置的更改,主要是監控屏幕方向的更改
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		String screen = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ? "橫向屏幕"
				: "豎向屏幕";
		Toast.makeText(this, "\n修改後的屏幕方向爲" + screen, 1).show();
	}
}


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/bn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="更改屏幕方向" />

</LinearLayout>


爲了讓Activity能監聽屏幕方向的更改的時間,需要在配置改Activity時指定acdroid:configChanges屬性,該屬性支持的其中一種屬性值爲orientation,指定Activity可以監聽屏幕方向改變的事件。


android:targetSdkVersion="12"最高只能設爲12,不然無法監聽系統設置的更改。


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.changecfg"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="12" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.changecfg.MainActivity"
            android:configChanges="orientation"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章