วันอาทิตย์ที่ 23 มกราคม พ.ศ. 2554

Lock Screen Orientation in Android

REFERENCE : Lock Screen Orientation in Android


[Code sample – How to lock the orientation]
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}


[Code sample – How to detect the current orientation]
switch (this.getResources().getConfiguration().orientation)
{
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;
}

[Code sample – Locking rotation while performing an action]

// Sets screen rotation as fixed to current rotation setting
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;
}
}

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

ไม่มีความคิดเห็น:

แสดงความคิดเห็น