API文档:Configuration:http://androiddoc.qiniudn.com/reference/android/content/res/Configuration.html
1.Configuration给我们提供的方法列表 densityDpi:屏幕密度 fontScale:当前用户设置的字体的缩放因子 hardKeyboardHidden:判断硬键盘是否可见,有两个可选值:HARDKEYBOARDHIDDEN_NO,HARDKEYBOARDHIDDEN_YES,分别是十六进制的0和1 keyboard:获取当前关联额键盘类型:该属性的返回值:KEYBOARD_12KEY(只有12个键的小键盘)、KEYBOARD_NOKEYS、KEYBOARD_QWERTY(普通键盘) keyboardHidden:该属性返回一个boolean值用于标识当前键盘是否可用。该属性不仅会判断系统的硬件键盘,也会判断系统的软键盘(位于屏幕)。 locale:获取用户当前的语言环境 mcc:获取移动信号的国家码 mnc:获取移动信号的网络码 ps:国家代码和网络代码共同确定当前手机网络运营商 navigation:判断系统上方向导航设备的类型。该属性的返回值:NAVIGATION_NONAV(无导航)、 NAVIGATION_DPAD(DPAD导航)NAVIGATION_TRACKBALL(轨迹球导航)、NAVIGATION_WHEEL(滚轮导航) orientation:获取系统屏幕的方向。该属性的返回值:ORIENTATION_LANDSCAPE(横向屏幕)、ORIENTATION_PORTRAIT(竖向屏幕) screenHeightDp,screenWidthDp:屏幕可用高和宽,用dp表示 touchscreen:获取系统触摸屏的触摸方式。该属性的返回值:TOUCHSCREEN_NOTOUCH(无触摸屏)、TOUCHSCREEN_STYLUS(触摸笔式触摸屏)、TOUCHSCREEN_FINGER(接收手指的触摸屏)
代码实现: public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView txtResult = (TextView) findViewById(R.id.txtResult); StringBuffer status = new StringBuffer(); //①获取系统的Configuration对象 Configuration cfg = getResources().getConfiguration(); //②想查什么查什么 status.append("densityDpi:" + cfg.densityDpi + " "); status.append("fontScale:" + cfg.fontScale + " "); status.append("hardKeyboardHidden:" + cfg.hardKeyboardHidden + " "); status.append("keyboard:" + cfg.keyboard + " "); status.append("keyboardHidden:" + cfg.keyboardHidden + " "); status.append("locale:" + cfg.locale + " "); status.append("mcc:" + cfg.mcc + " "); status.append("mnc:" + cfg.mnc + " "); status.append("navigation:" + cfg.navigation + " "); status.append("navigationHidden:" + cfg.navigationHidden + " "); status.append("orientation:" + cfg.orientation + " "); status.append("screenHeightDp:" + cfg.screenHeightDp + " "); status.append("screenWidthDp:" + cfg.screenWidthDp + " "); status.append("screenLayout:" + cfg.screenLayout + " "); status.append("smallestScreenWidthDp:" + cfg.densityDpi + " "); status.append("touchscreen:" + cfg.densityDpi + " "); status.append("uiMode:" + cfg.densityDpi + " "); txtResult.setText(status.toString()); } }
3.重写onConfigurationChanged响应系统设置更改
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.btncahange); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { 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); } } }); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); String screen = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE?"横屏":"竖屏"; Toast.makeText(MainActivity.this, "系统屏幕方向发生改变 修改后的方向为" + screen, Toast.LENGTH_SHORT).show(); } } 另外,还需要在AndroidManifest.xml添加下述内容: 权限: < uses-permission android:name="android.permission.CHANGE_CONFIGURATION" /> 在< activity标签中添加:android:configChanges="orientation" 将targetSdkVersion改为12以上的,12也可以