通过修改数据库System settings 来修改手机屏幕的旋转方向, 设置Settings.System. ACCELEROMETER_ROTATIONSettings.System. USER_ROTATION的值,可以通过命令行和Java API来实现

1.通过命令行来修改

   
  1. su 
  2. sqlite3 /data/data/com.android.providers.settings/databases/settings.db 
  3. insert into system (name,value) values('accelerometer_rotation',0); 
  4. insert into system (name,value) values('user_rotation',1); 
  5. .exit 

其中,user_rotation的值和旋转角度的关系如下,

 

  • user_rotation 0 -> ROTATION_0
  • user_rotation 1 -> ROTATION_180
  • user_rotation 2 -> ROTATION_270
  • user_rotation 3 -> ROTATION_90

2.通过java 程序实现

首先取消手机的重力感应(0 禁用,1 开启)

 

    
  1. public void disableAccelerometerRotation(){ 
  2.         
  3. try { 
  4.  
  5.      Settings.System.putInt(mcontext.getContentResolver(),Settings.System. ACCELEROMETER_ROTATION,0); 
  6.  
  7.  } catch (Exception e) { 
  8.  
  9.             e.printStackTrace(); 
  10.  } 
  11.        

设置指定的屏幕旋转方向

   
  1. public void setScreenRotation(String rotationStr){ 
  2.           
  3.           int rotation = 0;// Surface.ROTATION_90; 
  4.           
  5.           if(rotationStr.equals("0" )){ 
  6.                 
  7.                rotation = Surface. ROTATION_0; 
  8.                 
  9.          } else if (rotationStr.equals("90")){ 
  10.                 
  11.                rotation = Surface. ROTATION_90; 
  12.                 
  13.          } else if (rotationStr.equals("180")){ 
  14.                 
  15.                rotation = Surface. ROTATION_180; 
  16.                 
  17.          } else if (rotationStr.equals("270")){ 
  18.                 
  19.                rotation = Surface. ROTATION_270; 
  20.          } 
  21.                            
  22.          Settings.System. putInt(mcontext.getContentResolver(),Settings.System. USER_ROTATION,rotation); 
  23.  
  24.   } 

 因为修改了Settings的值,所以在程序中添加权限声明:



本文出自 “whithin's” 博客,请务必保留此出处http://whithin.blog.51cto.com/690417/1066779

更多相关文章

  1. Fix bug in the notepad tutorial of Android
  2. Android(安卓)-- 点击双下返回退出程序
  3. android BOOT_COMPLETED 失败
  4. 反编译Android部署程序
  5. Android(安卓)系统应用调用,intent的使用总结帖
  6. Android(安卓)URI简单介绍
  7. android 系统开发 学习方向
  8. Google Play 商店删除百度的两个Android应用程序
  9. Android应用程序进程启动源码解析

随机推荐

  1. 第一节课:浅谈http
  2. php输出数组变成了对象?php数组转化成json
  3. 前端 JavaScript 中的三种 for 循环语句
  4. 常用 Git 命令总结
  5. Vscode安装和常用插件装置与个人对HTTP协
  6. html元素,怎么通过JS函数操作;thinkphp模板
  7. php学习
  8. 下载安装VS Code 及常用插件,与个人对Http
  9. 初次学习前端必备的开发工具VS Code简单
  10. 2021.6.28