Configuration类的用法

本文介绍如何使用Android的Configuration类来获取设备的配置信息,包括屏幕方向、触摸屏支持等,并提供了一个简单的示例应用程序来展示如何读取这些配置。

Configuration类专门用于描述手机设备上的配置信息,这些配置信息既包括用户特定的配置项,也包括系统的动态设备配置,程序可调用Activity的如下方法来获取系统的Configuration对象:

Configuration cfg=getRosources().getConfiguration();

实例:获取系统设备状态

该实例界面布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.home.planefly.MainActivity">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ori"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/navigation"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/touch"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/mnc"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bn"
        android:text="@string/getInformation"/>
</LinearLayout>

java代码如下:

public class MainActivity extends AppCompatActivity {

    EditText ori;
    EditText navigation;
    EditText touch;
    EditText mnc;
    @Override
    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);
        Button bn=(Button)findViewById(R.id.bn);
        bn.setOnClickListener(new View.OnClickListener() {
            @Override
            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
                        ?"方向键控制方向":"轨迹球控制方向";
                navigation.setText(naviName);
                String touchName=cfg.touchscreen==Configuration.TOUCHSCREEN_NOTOUCH
                        ?"无触摸屏":"支持触摸屏";
                ori.setText(screen);
                mnc.setText(mncCode);
                touch.setText(touchName);
            }
        });
    }
}
以上;

仅供以后参考之用。

在 Java 应用程序中,`Configuration` 是一种用于读取和管理配置信息的工具。它简化了应用程序从配置文件中加载参数的过程,并提供了灵活的方式来访问这些参数[^1]。该通常与 `Properties` 或自定义的配置解析机制结合使用,以便在运行时动态获取配置值。 ### 基本用法 一个典型的 `Configuration` 实现可能包括加载配置文件、存储键值对以及提供便捷的方法来检索这些值。以下是一个简单的示例: ```java import java.io.InputStream; import java.util.Properties; public class Configuration { private Properties properties = new Properties(); public Configuration(String configFileName) { try (InputStream input = getClass().getClassLoader().getResourceAsStream(configFileName)) { if (input == null) { throw new RuntimeException("配置文件未找到: " + configFileName); } properties.load(input); } catch (Exception e) { throw new RuntimeException("无法加载配置文件", e); } } public String getProperty(String key) { return properties.getProperty(key); } public int getIntProperty(String key) { return Integer.parseInt(properties.getProperty(key)); } public boolean getBooleanProperty(String key) { return Boolean.parseBoolean(properties.getProperty(key)); } } ``` 使用此 `Configuration` 时,首先需要传入配置文件的路径(例如 `config.properties`),然后通过 `getProperty()` 方法获取特定的配置项。例如: ```java public class Main { public static void main(String[] args) { Configuration config = new Configuration("config.properties"); String dbHost = config.getProperty("database.host"); int dbPort = config.getIntProperty("database.port"); boolean debugMode = config.getBooleanProperty("application.debug"); System.out.println("数据库地址:" + dbHost); System.out.println("数据库端口:" + dbPort); System.out.println("调试模式:" + debugMode); } } ``` ### 配置文件示例 对应的 `config.properties` 文件内容如下: ```properties database.host=localhost database.port=3306 application.debug=true ``` ### 高级特性 除了基本的键值对读取功能外,`Configuration` 还可以扩展以支持更复杂的配置结构,例如嵌套对象、列表等。这通常涉及使用 JSON 或 YAML 格式的配置文件,并借助第三方库(如 Jackson 或 SnakeYAML)进行解析。 此外,一些框架(如 Apache Commons Configuration)提供了更加丰富且型安全的配置管理接口,支持自动重载、多配置源合并等功能。这高级配置管理方案适用于需要动态调整配置参数的复杂系统。 ### Spring 中的 @Configuration 注解 在 Spring 框架中,`@Configuration` 注解用于标识某个为配置,可以替代传统的 XML 配置文件[^2]。此中的每个方法通常返回一个 Bean 实例,并由 Spring 容器负责管理其生命周期。例如: ```java @Configuration public class AppConfig { @Bean public DataSource dataSource() { return new DriverManagerDataSource("jdbc:mysql://localhost:3306/mydb", "user", "password"); } } ``` 上述代码表示 `AppConfig` 是一个配置,并且其中的 `dataSource()` 方法将被 Spring 用来创建并注册一个 `DataSource` Bean。 ### .NET 中的 ConfigurationManager 在 .NET 平台上,`ConfigurationManager` 提供了似的功能,用于从 `App.config` 或 `Web.config` 文件中读取配置信息[^3]。以下是一个典型的使用方式: ```csharp using System; using System.Configuration; class Program { static void Main() { string settingValue = ConfigurationManager.AppSettings["SettingName"]; Console.WriteLine($"SettingName: {settingValue}"); } } ``` 在此示例中,`AppSettings` 属性用于读取 `<appSettings>` 节点下的键值对配置。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值