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="fill_parent"
android:layout_height="wrap_content"
android:hint="屏幕方向"
android:editable="false"
/>
<EditText
android:id="@+id/navigation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="方向控制"
android:editable="false"
/>
<EditText
android:id="@+id/touch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="有无触屏"
android:editable="false"
/>
<EditText
android:id="@+id/mnc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="移动信号网络码"
android:editable="false"
/>
<Button
android:id="@+id/bn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取手机信息"
android:layout_gravity="center"
/>
</LinearLayout>
ConfigurationText.java
package com.example.configurationtest;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class ConfigurationTest extends Activity {
EditText ori;
EditText navigation;
EditText touch;
EditText mnc;
Button bn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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()
{
@Override
public void onClick(View source) {
// TODO Auto-generated method stub
Configuration cfg = getResources().getConfiguration();
String screen = cfg.orientation ==
Configuration.ORIENTATION_LANDSCAPE?"横向屏幕":"竖向屏幕";
String mncCode = cfg.mnc + "";
String naviName = cfg.navigation ==
Configuration.NAVIGATION_NONAV
?"无方向控制":cfg.orientation ==
Configuration.NAVIGATION_WHEEL
?"滚轮控制":cfg.orientation ==
Configuration.NAVIGATION_DPAD
?"方向键控制方向":"轨迹球控制方向";
String touchName = cfg.touchscreen ==
Configuration.TOUCHSCREEN_NOTOUCH
?"无触摸屏":"支持触摸屏";
navigation.setText(naviName);
ori.setText(screen);
mnc.setText(mncCode);
touch.setText(touchName);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.configuration_test, menu);
return true;
}
}
代码运行示例图:
程序可调用Activity的如下方法来获取系统的Configuration对象:
Configuration cfg = getResources().getConfiguration();
一旦获得了系统的Configuration对象,该对象提供了如下常用属性来获取系统的配置信息。