Android开发sensor旋转屏问题如何解决

其他教程   发布日期:2023年07月31日   浏览次数:412

这篇文章主要讲解了“Android开发sensor旋转屏问题如何解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android开发sensor旋转屏问题如何解决”吧!

一、查询 activity xml 配置screenOrientation信息:

找个测试机器,安装此APP。as中 devices explorer界面中打开此apk安装目录,双击apk。或者把apk托到as中,在反编译的资产xml文件搜索此activity,并查看 android:screenOrientation 属性。或者使用 aapt dump xmltree 命令查看。

configChanges 属性是为了防止重启activity

  1. <activity
  2. android:theme="@ref/0x7f130170"
  3. android:label="@ref/0x7f1200c6"
  4. android:name="com.miui.gallery.activity.ExternalPhotoPageActivity"
  5. android:screenOrientation="4"
  6. android:configChanges="0xda0"
  7. android:showWhenLocked="true">

二、事件发生时间点分析:

事件发生时间点=录屏文件名的时间 + 录屏发生时视频经过的秒数

结合问题描述和usagestats服务的信息倒推事件发生时间点

三、logcat查看sensor激活状态:

此log是高通 sensors-hal 层打印的

状态栏--控制按钮--“方向锁定” 开启时,en=0 即 sensor关闭!

/vendor/qcom/proprietary/sensors-see/sensors-hal-2.0/framework/sensors_hal.cpp

logcat中搜索

  1. device_orientation
  1. 2021-08-09 17:07:23.037 1021-1021/? I/sensors-hal: activate:209, android.sensor.device_orientation/272 en=1
  2. 2021-08-09 17:07:23.048 1021-1021/? I/sensors-hal: activate:220, android.sensor.device_orientation/272 en=1 completed

四、查看APP是否注册旋转屏sensor

有的APP注册的是加速度sensor,使用自己的算法计算

APP注册 android.view.OrientationEventListener 回调,自身代码调节旋转Demo:

  1. private void registerOrientation(){
  2. OrientationEventListener orientationListener = new OrientationEventListener(this) {
  3. @Override
  4. public void onOrientationChanged(int orientation) {
  5. Log.d(TAG, "onOrientationChanged: " + orientation);
  6. if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
  7. return; // 手机平放时,检测不到有效的角度
  8. }
  9. // 设置竖屏
  10. if (((orientation >= 0) && (orientation <= 30)) || (orientation >= 330)) {
  11. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  12. }
  13. // 设置横屏
  14. else if (((orientation >= 230) && (orientation <= 310))) {
  15. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  16. }
  17. }
  18. };
  19. orientationListener.enable();
  20. }

五、sensorservice 信息

DUMP OF SERVICE CRITICAL sensorservice:

bugreport dumpsys sensorservice信息查看 注册的sensor信息

  1. Previous Registrations:
  2. 17:57:12 + 0x0000002d pid=20420 uid=10265 package=android.view.OrientationEventListener samplingPeriod=200000us batchingPeriod=0us
  3. 17:57:12 + 0x0000002d pid=20420 uid=10265 package=android.view.OrientationEventListener samplingPeriod=200000us batchingPeriod=0us

相关sensor:

  1. // 加速计传感器 handle: 0x0100000b
  2. // app通常会注册回调 android.view.OrientationEventListener 此类内部注册了 加速计sensor,用来监听旋转角度
  3. // android.sensor.accelerometer
  4. public static final int TYPE_ACCELEROMETER = 1;
  5. 0x0100000b) lsm6dso Accelerometer Non-wakeup | STMicro | ver: 142870 | type: android.sensor.accelerometer(1) | perm: n/a | flags: 0x00000980
  6. continuous | minRate=1.00Hz | maxRate=415.97Hz | FIFO (max,reserved) = (10000, 3000) events | non-wakeUp |
  7. highest rate level = 3, support shared mem: gralloc,
  8. 0x0100015f) lsm6dso Accelerometer-Uncalibrated Non-wakeup | STMicro | ver: 142870 | type: android.sensor.accelerometer_uncalibrated(35) | perm: n/a | flags: 0x00000980
  9. continuous | minRate=1.00Hz | maxRate=415.97Hz | FIFO (max,reserved) = (10000, 3000) events | non-wakeUp |
  10. highest rate level = 3, support shared mem: gralloc,
  11. 0x5f676273) Gyroscope Bias (debug) | AOSP | ver: 1 | type: android.sensor.accelerometer(1) | perm: n/a | flags: 0x00000000
  12. continuous | maxDelay=0us | maxRate=415.97Hz | no batching | non-wakeUp |
  13. // 方向传感器---用户app旋转屏 handle: 0x0100010f
  14. // android.sensor.device_orientation
  15. public static final int TYPE_DEVICE_ORIENTATION = 27;
  16. 0x0100010f) device_orient Non-wakeup | xiaomi | ver: 1 | type: android.sensor.device_orientation(27) | perm: n/a | flags: 0x00000002
  17. on-change | maxDelay=0us | minDelay=0us | FIFO (max,reserved) = (10000, 0) events | non-wakeUp |
  18. // 其 Wakeup handle: 0x01000110 com.android.server.policy.WindowOrientationListener 注册此类型
  19. 0x01000110) device_orient Wakeup | xiaomi | ver: 1 | type: android.sensor.device_orientation(27) | perm: n/a | flags: 0x00000003
  20. on-change | maxDelay=0us | minDelay=0us | FIFO (max,reserved) = (10000, 0) events | wakeUp |

bugreport device_orient 信息查看上报事件

  1. # 0 表示通常的正常
  2. # 1 表示顺时针旋转90度
  3. device_orient Wakeup: last 10 events
  4. 1 (ts=68477.447943014, wall=17:49:58.724) 0.00,
  5. 2 (ts=68478.554363222, wall=17:49:59.831) 0.00,
  6. 3 (ts=68478.989033691, wall=17:50:00.265) 0.00,
  7. 4 (ts=68479.423711660, wall=17:50:00.701) 1.00,
  8. 5 (ts=68482.743111608, wall=17:50:04.022) 0.00,
  9. 6 (ts=68486.101931400, wall=17:50:07.379) 0.00,
  10. 7 (ts=68495.309003118, wall=17:50:16.585) 0.00,
  11. 8 (ts=68505.308247806, wall=17:50:26.584) 1.00,
  12. 9 (ts=68507.166153170, wall=17:50:28.444) 0.00,
  13. 10 (ts=68513.214244108, wall=17:50:34.491) 0.00,

六、有现场,查看当前activity信息

logcat查看当前activity: 有很多其他日志可供查看

  1. 07-31 17:50:25.687563 4486 4486 D Nav_RecentsImpl: onResumed className=com.miui.gallery.activity.ExternalPhotoPageActivity,mIsInAnotherPro=false,isKeyguardLocked=false,mNavStubView=com.miui.home.recents.NavStubView{f994da4 G.E...... ......I. 0,0-1080,35}

根据DUMP OF SERVICE usagestats:内容 查看时间点的APP

  1. time="2021-07-08 09:29:02" type=ACTIVITY_PAUSED package=com.ainemo.dragoon class=com.meeting.call.activity.XylinkMeetingActivity instanceId=40437940 taskRootPackage=com.ainemo.dragoon taskRootClass=com.ainemo.android.activity.MainActivity flags=0x0
  2. log的结尾:
  3. In-memory daily stats
  4. timeRange="2021/8/21 08:14

以上就是Android开发sensor旋转屏问题如何解决的详细内容,更多关于Android开发sensor旋转屏问题如何解决的资料请关注九品源码其它相关文章!