Android中Configuration类简介

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

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>


附上图片效果:

获取手机信息前:

获取手机信息后:

### 如何在Android项目中添加配置 在Android项目中,可以通过多种方式来定义和管理项目的配置。这些配置可以用于指定编译选项、依赖项版本控制以及应用运行时的行为调整。 #### 1. 使用`gradle.properties`文件 可以在根目录下的`gradle.properties`文件中定义全局变量,以便在整个构建过程中重复使用。例如: ```properties ANDROID_BUILD_SDK_VERSION=30 ANDROID_BUILD_MIN_SDK_VERSION=21 ANDROID_BUILD_TARGET_SDK_VERSION=30 ANDROID_BUILD_TOOLS_VERSION=30.0.3 ``` 随后,在模块级别的`build.gradle`文件中引用这些属性[^1]: ```groovy android { compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION) buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION defaultConfig { minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION) targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION) } } ``` 这种方式有助于集中管理和维护不同环境中的SDK版本和其他参数。 --- #### 2. 定义自定义键值对 如果需要为特定功能提供配置支持,则可利用`CarrierConfigManager`实现动态加载配置数据[^2]。此方法适用于运营商定制化场景或其他需通过API获取实时更新的应用需求。 例如: ```java import android.telephony.CarrierConfigManager; public class CustomConfigLoader { private static final String KEY_MY_CUSTOM_CONFIG = "my_custom_config_key"; public boolean getCustomBooleanValue(Context context) { CarrierConfigManager configManager = (CarrierConfigManager) context.getSystemService(Context.CARRIER_CONFIG_SERVICE); PersistableBundle bundle = configManager.getConfig(); if (bundle != null && bundle.containsKey(KEY_MY_CUSTOM_CONFIG)) { return bundle.getBoolean(KEY_MY_CUSTOM_CONFIG, false); } return false; } } ``` 注意:此配置仅限于某些预设范围内的密钥名称列表,并且其具体含义可能随平台升级而有所变化。 --- #### 3. 配置外部库(如Akka) 当集成第三方框架至应用程序内部结构时,通常还需要额外考虑它们各自的初始化逻辑。比如对于基于消息传递模式设计的分布式计算引擎——Akka而言,可通过如下形式完成基本设定[^3]: ```java ActorSystem system = ActorSystem.create("MyApplication", ConfigFactory.load().getConfig("application")); ActorRef router = system.actorOf( Props.create(MyWorker.class).withRouter(new RoundRobinPool(5)), "roundrobinrouter"); ``` 这里展示了创建一个简单的轮询调度器实例的过程;其中涉及到了读取名为“application”的Hocon格式资源文档作为输入源之一。 --- #### 4. Windows Forms样式的DI容器注册服务 尽管主要讨论的是安卓开发领域的内容,但为了对比说明跨平台差异性,下面给出一段关于.NET Core环境下如何借助依赖注入机制绑定业务组件的例子[^4]: ```csharp public void ConfigureServices(IServiceCollection services) { services.AddXaf(Configuration, builder => { MyCustomModule.SetupObjectSpace(builder.ObjectSpaceProviders); }); } ``` 虽然两者技术栈完全不同,但从概念层面看都是围绕着灵活扩展性和松耦合架构展开的设计思路体现。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值