出自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.

[Code sample – How to lock the orientation]
/**Calledwhentheactivityisfirstcreated.*/
@Override
public void onCreate ( BundlesavedInstanceState )
{
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.

[Code sample – How to detect the current orientation]
switch ( this .getResources () .getConfiguration () .orientation )
{
case Configuration.ORIENTATION_PORTRAIT:
//Dosomethinghere
break ;
case Configuration.ORIENTATION_LANDSCAPE:
//Dosomethinghere
break ;
case Configuration.ORIENTATION_SQUARE:
//Dosomethinghere
break ;
default :
thrownew Exception ( "Unexpectedorientationenumerationreturned" ) ;
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.

[Code sample – Locking rotation while performing an action]
//Setsscreenrotationasfixedtocurrentrotationsetting
private void mLockScreenRotation ()
{
//Stopthescreenorientationchangingduringanevent
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.

[Code sample – How to re-enable screen rotation]
//allowscreenrotationsagain
this .setRequestedOrientation (
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED ) ;

异步处理 的时候如果转屏 异步处理就会停止,附件就是防止这种情况发生

更多相关文章

  1. [Android] 升级了新的android studio之后 发生如下的报错,The fol
  2. android发送QQ邮件(带附件)
  3. android gmail 添加附件(转载)
  4. Android 同时播放缩放动画和位移动画 时, 位移的路径会发生偏移.
  5. 转 :android gmail 添加附件
  6. android webview增加下载附件功能
  7. 作为Android开发者 你真的知道Android按下开机键到启动发生什么
  8. Android发送邮件到指定邮箱(可带附件)

随机推荐

  1. Android FragmentTransactionExtended:使F
  2. 重温android studio jni编译生成so文件
  3. Android中解析JSON的两种方式
  4. Andoroid学习笔记05——Activity布局管理
  5. Android(安卓)Audio 框架简读
  6. Android最新敲诈者病毒分析及解锁(11月版)
  7. android判断是否联网
  8. Android ADB始终为unautorized的解决
  9. Android Studio 使用本地的gradle
  10. app性能优化