Android Lock Screen Orientation
一些与屏幕有关的基础知识: //这个是手机屏幕的旋转角度
final int rotation = this.getWindowManager().getDefaultDisplay().getOrientation(); rotation值有:
Surface.ROTATION_0
Surface.ROTATION_90
Surface.ROTATION_180
Surface.ROTATION_270 //这个是读取当前Activity 的屏幕方向
final int orientation = this.getResources().getConfiguration().orientation; orientation值有:
Configuration.ORIENTATION_PORTRAIT
Configuration.ORIENTATION_LANDSCAPE //这个是设置Activity 的屏幕方向,与AndroidManifest.xml中的android:screenOrientation="landscape"配置等同
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
Build.VERSION:Various version strings.
android.os.Build.VERSION.SDK_INT:The user-visible SDK version of the framework; its possible values are defined in Build.VERSION_CODES
.
Build.VERSION_CODES:Enumeration of the currently known SDK version codes. These are the values that can be found in SDK
. Version numbers increment monotonically with each official platform release.
android.os.Build.VERSION_CODES.FROYO:June 2010: Android 2.2
The REVERSE_LANDSCAPE and REVERSE_PORTRAIT appear after android 2.3,so we can not use them on android 2.2 and former devices.

private void disableRotation()
{
final int orientation = getResources().getConfiguration().orientation;
final int rotation = getWindowManager().getDefaultDisplay().getOrientation(); // Copied from Android docs, since we don't have these values in Froyo 2.2
int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;
int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9; if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)
{
SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
} if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)
{
if (orientation == Configuration.ORIENTATION_PORTRAIT){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else if (orientation == Configuration.ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270)
{
if (orientation == Configuration.ORIENTATION_PORTRAIT){
setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
else if (orientation == Configuration.ORIENTATION_LANDSCAPE){
setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
}
}

enable the rotation:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
About the rotation and orientation,on some devices,the Surface.ROTATION_0 corresponds to ORIENTATION_LANDSCAPE,but others,may ORIENTATION_PORTRAIT.So we have to so some work to judge it.
However,for some reason ROTATION_90 corresponds to SCREEN_ORIENTATION_REVERSE_PORTRAIT on the Xoom if you use the above method.So another method appears:

private void disableRotation()
{
final int orientation = getResources().getConfiguration().orientation;
final int rotation = getWindowManager().getDefaultDisplay().getOrientation(); // Copied from Android docs, since we don't have these values in Froyo 2.2
int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;
int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9; if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)
{
SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
} if (orientation == Configuration.ORIENTATION_PORTRAIT) {
if (rotation == Surface.ROTATION_0|| rotation == Surface.ROTATION_270)
// 0 for phone, 270 for tablet
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
} else {
if (rotation == Surface.ROTATION_90|| rotation == Surface.ROTATION_0)
// 90 for phone, 0 for tablet
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
} }

i'm not sure the note "//0 for phone, 270 for tablet" and "// 90 for phone, 0 for tablet" are right on any device,but at least it works well in my device.so I use the latter one.
When you want to disable/enable the autorotation of the device ,you have to write the settings.
Android Lock Screen Orientation的更多相关文章
- HTML5: Screen Orientation API
媒体的询问取决于智能手机和平板布局调整的方向一致网站.但有时候你被锁定在一个希腊网站特定方向.横向或纵向.此时,是本机格式可以指定保健应用. APP只显示在一个预设格式-独立于实际设备方向.通过使用H ...
- 查看,设置,设备的 竖屏-横屏模式 screen.orientation
<body> <div id="doc"></div> <div id="model"></div> ...
- App/Activity/Screen Orientation
测试android屏幕方向的小Demo 1.首先我们在values下面新建文件arrays.xml(用来在下拉列表中显示) <?xml version="1.0" encod ...
- 屏幕方向读取与锁定:Screen Orientation API(转)
什么是 Screen Orientation API Screen Orientation API 为 Web 应用提供了读取设备当前屏幕方向.旋转角度.锁定旋转方向.获取方向改变事件的能力.使得特定 ...
- [Android Pro] 横竖屏切换时,禁止activity重新创建,android:configChanges="keyboardHidden|orientation" 不起作用
referece to : http://blog.csdn.net/mybook1122/article/details/24978025 这个网上搜索,很多结果都是: AndroidManifes ...
- Windows Phone Bing lock screen doesn't change解决方法
之前一直用的Lumia 925,Bing lock screen每天都会更换.这几天换了Lumia 930,同步了账号相关的设置,发现Bing lock screen不再每天更换.尝试重启.使用cel ...
- 【转】ANDROID LOLLIPOP SCREEN CAPTURE AND SHARING
https://datatheorem.github.io/android/2014/12/26/android-screencapture/ https://www.youtube.com/watc ...
- Android Screen Orientation
Ref:Android横竖屏切换小结 Ref:Android游戏开发之横竖屏的切换(二十七)
- Android Screen Orientation Change (Screen Rotation) Example
原文见: http://techblogon.com/android-screen-orientation-change-rotation-example/#
随机推荐
- (.iso)光盘镜像文件的打开与安装
直接解压就可以打开,然后就可以安装.exe文件
- hdu 1847 Good Luck in CET-4 Everybody!(简单博弈SG)
#include<stdio.h> #include<string.h> #define N 1010 int hash[N]; int sg[N]; void GetSG() ...
- java中String,StringBuffer,StringBuilder之间的区别
文章转载自:http://www.cnblogs.com/frankliiu-java/archive/2010/07/05/1771537.html String是固定长度的字符串,如果要发生变化必 ...
- IE9 JSON未定义
原文:http://social.msdn.microsoft.com/Forums/ie/en-US/fc41127c-0243-4d2e-8e7c-2b311f12e390/ie9-json-no ...
- JavaWeb项目开发案例精粹-第4章博客网站系统-005action层
1. package com.sanqing.action; import java.util.Date; import java.util.Map; import com.opensymphony. ...
- Crypto++编译使用
简述 Crypto++库是一个用c++ 编写的密码类库,是一个自由软件.有关它的信息可以访问以下两个网站: Crypto++® Library Wiki-Crypto++® Library 简述 下载 ...
- Java:IO流之字符流缓冲区详解
字符流缓冲区: 1.缓冲区的出现提高了对数据的读写效率: 2.对应类:BufferedWriter.BufferedReader: 3.缓冲区要结合流才可以使用: 4.在流的基础上对流的功能进行了增强 ...
- Java:IO流之字节流InputStream、OutputStream详解
字节流: (抽象基类)InputStream类(读): (抽象基类)OutputStream类(写): InputStream: 构造方法摘要 InputStream() ...
- Android 去掉title bar的3个方法
1. Java代码实现 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstance ...
- C# 窗体间传值方法大汇总
第一种方法:创建一个类,里面声明用于存储接收的字段.传的时候存储于字段中,要用的时候,直接类名.字段名 进行调用.(这种方法传递是双向的) 第二种方法:1.在Form1里定义 public strin ...