原文转自:http://www.chengyunfeng.com/2010/02/how-to-obtain-the-equipment-the-width-and-height
//在某些场所下可能需要获取手机屏幕的宽度和高度,可以通过如下代码获取: DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); mScreenWidth = dm.widthPixels; mScreenHeight = dm.heightPixels; //也可通过getSystemService来获取WindowManager: WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); Display display = manager.getDefaultDisplay(); Pont screenResolution = new Point(display.getWidth(), display.getHeight());
出自http://eigo.co.uk/News-Article.aspx?NewsArticleID=103
How to lock the orientation
In the onCreateDialog(int) event of the activity use the setRequestedOrientation(int) method to set the screen orientation to your chosen setting. The activity will stay in this orientation regardless of if the device is tilted or not.
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
How to detect the current orientation
To programmatically detect the current orientation of the activity use the following code snippet. The orientation property of the Configuration class returns three possible values corresponding to Landscape, Portrait and Square.
{
case Configuration.ORIENTATION_PORTRAIT:
// Do something here
break;
case Configuration.ORIENTATION_LANDSCAPE:
// Do something here
break;
case Configuration.ORIENTATION_SQUARE:
// Do something here
break;
default:
throw new Exception("Unexpected orientation enumeration returned");
break;
}
Example : Locking rotation while performing an action.
You might wish to disable the screen rotation whilst performing an action or by user command, to do this you need to combine the above samples to detect the current orientation and lock the display to that orientation.
private void mLockScreenRotation()
{
// Stop the screen orientation changing during an event
switch (this.getResources().getConfiguration().orientation)
{
case Configuration.ORIENTATION_PORTRAIT:
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Configuration.ORIENTATION_LANDSCAPE:
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
}
}
Once your action has completed you may wish to enable screen rotation again, see the next section for an example on how to do this.
How to re-enable screen rotation
To enable the orientation to be automatically changed on device tilt simply pass thesetRequestedOrientation(int) method the enumeration value for an unspecified orientation.
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
异步处理 的时候如果转屏 异步处理就会停止,附件就是防止这种情况发生
在做项目时,偶尔写程序发现一个不经意的混乱事件!!如下代码:
public void onCreate(Bundle savedInstanceState) {
..
..
..
new Thread(new Runnable(){
public void run() {
// TODO Auto-generated method stub
btn_start_audio.setText("Confuse");
}
});
}
这时竟然可以运行成功,而且使button的text修改成功。没有报告"Only the original thread that created a view hierarchy can touch its views.“ 异常。
不过此次更改Button Text仍然是由UI线程完成的。因为在子线程启动之后,开始Run,而这时Button的Parent,或者是parent 的parent的(父视图的父视图) mParent.isLayoutRequested() 为true。导致子线程无法运行到 ViewRoot的requestLayout(),所以不会抛出上述异常。但是此时Button类里面的mText变量已经被更改了。当CPU切换到UI线程开始layout整个View 时,同时也就给Button 展现了一个 mText(刚才已经更新)。
new Thread(new Runnable(){
public void run() {
Thread.Sleep(500);
// TODO Auto-generated method stub
btn_start_audio.setText("Confuse");
}
});
}
这样就会出现异常。