//获得当前的屏幕方向
public static int ScreenOrient(Activity activity){
//取得当前屏幕的方向,如果此值为-1表示androidManifest.xml没有设置Android:screanOrentation属性所以这样无法判断屏幕方向。
//可以使用另一种思路,即长度大于高度的为横屏,否则为竖屏。
int orientation = activity.getRequestedOrientation();//得到屏幕方向
int landscape = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;//横屏静态常量
int portrait = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;//竖屏常量
int width = activity.getWindowManager().getDefaultDisplay().getWidth();//得到系统显示属性后得到屏幕宽度
int height = activity.getWindowManager().getDefaultDisplay().getHeight();//得到屏幕高度
return width>height?portrait:landscape;//判断
}
(2)避免Android系统自动重启activity,需要在Androidmanifest.xml中相应Activity加上android:configChanges="keyboardHidden|orientation"属性,使相应的Activity重启而是调用onConfigurationChanged(Configuration newConfig)。