一些与屏幕有关的基础知识:

//这个是手机屏幕的旋转角度
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的更多相关文章

  1. HTML5: Screen Orientation API

    媒体的询问取决于智能手机和平板布局调整的方向一致网站.但有时候你被锁定在一个希腊网站特定方向.横向或纵向.此时,是本机格式可以指定保健应用. APP只显示在一个预设格式-独立于实际设备方向.通过使用H ...

  2. 查看,设置,设备的 竖屏-横屏模式 screen.orientation

    <body> <div id="doc"></div> <div id="model"></div> ...

  3. App/Activity/Screen Orientation

    测试android屏幕方向的小Demo 1.首先我们在values下面新建文件arrays.xml(用来在下拉列表中显示) <?xml version="1.0" encod ...

  4. 屏幕方向读取与锁定:Screen Orientation API(转)

    什么是 Screen Orientation API Screen Orientation API 为 Web 应用提供了读取设备当前屏幕方向.旋转角度.锁定旋转方向.获取方向改变事件的能力.使得特定 ...

  5. [Android Pro] 横竖屏切换时,禁止activity重新创建,android:configChanges="keyboardHidden|orientation" 不起作用

    referece to : http://blog.csdn.net/mybook1122/article/details/24978025 这个网上搜索,很多结果都是: AndroidManifes ...

  6. Windows Phone Bing lock screen doesn't change解决方法

    之前一直用的Lumia 925,Bing lock screen每天都会更换.这几天换了Lumia 930,同步了账号相关的设置,发现Bing lock screen不再每天更换.尝试重启.使用cel ...

  7. 【转】ANDROID LOLLIPOP SCREEN CAPTURE AND SHARING

    https://datatheorem.github.io/android/2014/12/26/android-screencapture/ https://www.youtube.com/watc ...

  8. Android Screen Orientation

    Ref:Android横竖屏切换小结 Ref:Android游戏开发之横竖屏的切换(二十七)

  9. Android Screen Orientation Change (Screen Rotation) Example

    原文见: http://techblogon.com/android-screen-orientation-change-rotation-example/#

随机推荐

  1. 安卓开发利用外部jar包时ClassNotFound的错误

    今天调试一个小程序,一直没有进入 MainActivity 中的onCreate方法,因为没有看见提前设置好的log,然后仔细观察logcat的日志,发现提示很多ClassNotFound的信息,而且 ...

  2. P2P金融

    P2P金融又叫P2P信贷,是互联网金融(ITFIN)的一种.意思是:点对点. P2P金融指不同的网络节点之间的小额借贷交易(一般指个人),需要借助电子商务专业网络平台帮助借贷双方确立借贷关系并完成相关 ...

  3. windows条件下,Ping加上时间戳,并保存到文件,适用于测试网络

    在c盘下面新建文件 ping.vbs 在 ping.vbs中输入代码如下: Dim args, flag, unsuccOut args="" otherout="&qu ...

  4. iOS 开发--添加工程

    文/Bison(简书作者)原文链接:http://www.jianshu.com/p/dd71e15df5d0著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 第一部分,配置项目 在此只 ...

  5. 复习一下,? extends T 和 ? super T

    前话 最近学一些杂七杂八的东西,都把基础给忘了. 比如Java泛型中的 ? extends T和 ? super T 吧. 刚看开源项目的时候遇到它,表情如下: 源码分析 直接用源码来讲解吧 pack ...

  6. wordpress自定义栏目

    开启自定义栏目:点击头顶的“显示选项”,勾选“自定义栏目” 然后编辑文章时,即可看见 实验: 定义名称为:play_url ,值为:http://www.xiami.com/widget/635357 ...

  7. intellij idea搭建ssh开发框架之绑定数据源

    原文:intellij idea搭建ssh开发框架之绑定数据源 在intellij idea中绑定数据源并生成hibernate实体对象.在IDE中的右边找到Database标签. 点击弹出窗口中的图 ...

  8. Path Sum的变体

    早上看到一个面经题跟Path Sum很像, 给一个TreeNode root和一个target,找到一条从根节点到leaf的路径,其中每个节点和等于target. 与Path Sum不同是, Path ...

  9. Android 禁止进入activity自动弹出键盘

    在Manifest.xml中设定activity的属性 android:windowSoftInputMode="stateHidden|stateUnchanged" 附相关属性 ...

  10. 如何避免JSP页面自动生成session对象?为什么要这么做?

    JSP // 在默认情况下,在对一个JSP页面发出请求时,如果session还没有建立,JSP页面会自动为请求建立一个session对象,但是session是比较消耗资源的,如果没必要保持和使用ses ...