在开发Android应用时,有时候可能需要让应用程序随系统设置而进行调整,比如判断系统的屏幕方向、判断系统方向的方向导航设备等。除此之外,有时候可能还需要让应用程序监听系统设置的更改,对系统设置的更改做出响应。
一、Configuration类
Configuration类专门用于描述手机设备上的配置信息,这些配置信息既包括用户特定的配置项,也包括系统的动态设备配置。程序可调用Activity的如下方法来获取系统的Configuration对象:
Configuration cfg = getResources().getConfiguration();
一旦获得了系统的Configuration对象,就可以使用该对象提供的如下常用属性来获取系统的配置信息。
如果程序需要监听系统设置的更改,则可以考虑重写Activity的onConfigurationChanged (Configuration newConfig)方法,该方法是一个基于回调的事件处理方法:当系统设置发生更改时,该方法会被自动触发。
当然,为了让Activity能监听系统配置更改的事件,需要在配置Activity时指定 androidiconfigChanges 属性,该属性可以支持 mcc、mnc、locale、touchscreen、keyboard、keyboardHidden、navigation、orientation、screenLayout、uiMode、screenSize、smallestScreenSize、fontScale等属性值。
二、示例
接下来通过一个事例程序来学习如何获取设备的系统配置信息,以及如何响应系统设置更改。
同样使用WidgetSample工程,在app/main/res/layout/目录下创建system_event_layout.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:orientation="vertical">
<TextView
android:id="@+id/configuration_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
为了获取设备的配置信息,以及监听屏幕方向改变的事件,在java包下创建SystemEventActivity.java文件,加载上面新建的布局文件,具体代码如下:
package com.jinyu.cqkxzsxy.android.widgetsample;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
public class SystemEventActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.system_event_layout);
// 获取界面组件
TextView configurationTv = (TextView) findViewById(R.id.configuration_tv);
// 获取系统的Configuration对象
Configuration cfg = getResources().getConfiguration();
// 获取系统的配置信息
StringBuffer status = new StringBuffer();
status.append("屏幕密度:" + cfg.densityDpi + "\n");
status.append("字体缩放因子:" + cfg.fontScale + "\n");
status.append("硬键盘是否可见:" + cfg.hardKeyboardHidden + "\n");
status.append("键盘类型:" + cfg.keyboard + "\n");
status.append("当前键盘是否可用:" + cfg.keyboardHidden + "\n");
status.append("语言环境:" + cfg.locale + "\n");
status.append("移动信号的国家码:" + cfg.mcc + "\n");
status.append("移动信号的网络码:" + cfg.mnc + "\n");
status.append("方向导航设备的类型:" + cfg.navigation + "\n");
status.append("方向导航设备是否可用:" + cfg.navigationHidden + "\n");
status.append("系统屏幕的方向:" + cfg.orientation + "\n");
status.append("屏幕可用高:" + cfg.screenHeightDp + "\n");
status.append("屏幕可用宽:" + cfg.screenWidthDp + "\n");
status.append("系统触摸屏的触摸方式:" + cfg.densityDpi + "\n");
status.append("screenLayout:" + cfg.screenLayout + "\n");
status.append("smallestScreenWidthDp:" + cfg.densityDpi + "\n");
status.append("uiMode:" + cfg.densityDpi + "\n");
// 配置信息输出
configurationTv.setText(status.toString());
}
// 重写onConfigurationChanged方法,用于监听系统设置的更改
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// 获取更改后的屏幕方向
int screen = newConfig.orientation;
String ori = (Configuration.ORIENTATION_LANDSCAPE == screen) ? "横屏" : "竖屏";
// 消息提示
Toast.makeText(this, "系统的屏幕方向改变为:" + ori, Toast.LENGTH_SHORT).show();
}
}
为了让Activity能监听到屏幕方向的更改事件,需要在配置该Activity时指定 androidiconfigChanges 属性,应用的AndroidManifest.xml文件改为如下形式。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jinyu.cqkxzsxy.android.widgetsample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SystemEventActivity"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
上面的配置代码指定了该Activity可以监听屏幕方向改变的事件,这样当程序改变手机屏幕方向时,Activity的onConfigurationChanged()方法就会被回调。
运行程序,可以看到如下图所示系统配置。
在Genymotion模拟器上修改模拟器的屏幕方向,具体操作如下左图所示,当模拟器屏幕方向切换时,可以看到如下右图所示消息提示。
当然如果用真机测试,测试就更方便了。
关于Android系统事件的响应先学到这里,更多的内容后续再继续学习。