您的位置:首页 > 其它

Configuration类响应的系统设置的事件

2015-10-25 17:47 453 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息