Android 接近传感器问题仅在三星设备中 [英] Android proximity sensor issue only in Samsung devices
问题描述
避免问题的特定场景:
三星设备中的活动行为的不同之处在于,每次检测到变化时,对于接近度,它都会导致调用 onPause()/onResume()
仅在 SAMSUNG 设备上.>
我正在清除 onPause()
中的接近传感器,这导致了三星设备独有的行为.
希望这可以为任何面临此问题的人节省一些时间.我从 onPause()
中删除了清除邻近侦听器的调用,现在它可以在上述设备上按预期工作.
更新:
下面提到的不是唯一的问题,接近传感器的行为并不一致.另一个问题是 l-o-c:
if (!mWakeLock.isHeld()) mWakeLock.acquire();
随机地,if()
对上述设备返回 false,因此 mWakeLock.acquire()
并不总是被调用.
可能的相关信息:
mWakeLock = mPowerManager.newWakeLock(field, getLocalClassName());
哪里
field = PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK 或 32
<小时>
我的接近传感器代码仅在
上工作不正确1. 三星 GT-I9082 Android 4.2.2 (API 17) [二重奏]
2. 三星 SM-G925I Android 5.1.1 (API 22) [s6 edge]
代码:
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);传感器 = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);...新传感器事件监听器(){@覆盖公共无效 onSensorChanged(传感器事件事件){if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {if (event.values[0] <= 5) {//睡眠关闭屏幕();} else {//唤醒打开屏幕();} ...//使用 SensorManager.SENSOR_DELAY_NORMAL 注册监听器);
问题:两个设备上的接近传感器返回的记录值波动,即使我们将手一直放在手机上几秒钟;10-15 不移动它.
喜欢:
三星 6 edge-
02-10 20:12:36.532: D/SensorManager(3467): Proximity, val = 8.0 [far]02-10 20:12:36.532: D/SensorManager(29335): Proximity, val = 8.0 [far]02-10 20:12:36.532: D/DisplayPowerController(3467): [sensor] setProximitySensorEnabled::unregisterListener02-10 20:12:36.532: D/PowerManagerService(3467): [s] DisplayPowerCallbacks: onProximityNegative()02-10 20:12:36.562:I/传感器(3467):接近旧传感器_状态33554560,新传感器_状态:33554432 en:002-10 20:12:36.632:I/传感器(3467):接近旧传感器_状态 33554944,新传感器_状态:33555072 zh:102-10 20:12:36.642: D/SensorManager(29335): registerListener :: 5, TMD4903 接近传感器, 200000, 0,02-10 20:12:36.652: D/SensorManager(29335): Proximity, val = 8.0 [far]02-10 20:12:36.662: I/Sensors(3467): ProximitySensor - 8(cm)02-10 20:12:36.672: D/SensorManager(29335): Proximity, val = 8.0 [far]02-10 20:12:41.752:I/传感器(3467):接近旧传感器_状态 33554560,新传感器_状态:33554432 en:002-10 20:12:41.822:I/传感器(3467):接近旧传感器_状态 33554432,新传感器_状态:33554560 zh:102-10 20:12:41.842: D/SensorManager(29335): registerListener :: 5, TMD4903 接近传感器, 200000, 0,02-10 20:12:41.842: D/SensorManager(29335): Proximity, val = 8.0 [far]02-10 20:12:41.872: I/Sensors(3467): ProximitySensor - 8(cm)02-10 20:12:41.872: D/SensorManager(29335): Proximity, val = 8.0 [far]02-10 20:12:50.482: I/Sensors(3467): ProximitySensor - 0(cm)02-10 20:12:50.482: D/SensorManager(29335): Proximity, val = 0.0 [close]02-10 20:12:50.482: D/DisplayPowerController(3467): [sensor] setProximitySensorEnabled::registerListener02-10 20:12:50.482: D/SensorManager(3467): registerListener :: 5, TMD4903 接近传感器, 200000, 0,02-10 20:12:50.482: D/SensorManager(3467): Proximity, val = 0.0 [close]02-10 20:12:50.482: D/PowerManagerService(3467): [s] DisplayPowerCallbacks: onProximityPositive()02-10 20:12:50.482: D/PowerManagerService(3467): [s] DisplayPowerCallbacks: onProximityPositive()
Samsung Duos:0.0 和 5.0 的波动值与上面的 8.0 不同
还有哪些其他方法或排列组合可以成功、准确地解决这个问题?
是不是因为不同的传感器;GP2A接近传感器和APDS-9930/QPDS-T930接近&光?
已经花了很多时间了..徒劳
原来这是三星设备的注册问题,这里有一些页面可以帮助任何人解决这个问题:
使用onWindowFocusChanged.这对我来说很有意义,我与 onPause() 相关的实现也成功地遵循了相同的原则.
来源和解释:activity-onpause-to-handle-focus
什么帮助了我:而不是在onPause()
;@覆盖public void onWindowFocusChanged(boolean hasFocus) {super.onWindowFocusChanged(hasFocus);如果(!hasFocus){clearProximityListeners();}}
问题 74464:Activity#onPause 未调用Activity#onResume,或者可以 Context#registerReceiver 失败
更多参考:
从完全不同的方面来看,取消注册时的错误......奥托巴士......引用:对我们来说,这仅出现在三星设备上.经过快速搜索,...
如果答案更清晰,请编辑、更正、改进答案..希望能节省一些时间.
Specific scenario to avoid problems:
Behaviour for Activity in Samsung devices was different in the manner that every time there was a change detected, for proximity, it resulted in a call to onPause()/onResume()
ONLY on SAMSUNG devices.
I was clearing the proximity sensors in onPause()
which resulted in a behaviour unique to Samsung devices.
Hope this saves some time for anybody who's facing this. I removed the call of clearing proximity listeners from onPause()
and now it works as expected on the mentioned devices.
UPDATE:
What is mentioned below is not the only issue, the proximity sensor does not consistently behave in that manner. The other issue is a l-o-c:
if (!mWakeLock.isHeld()) mWakeLock.acquire();
Randomly, the if()
returns false for the mentioned devices hence mWakeLock.acquire()
isn't always called.
possibly relevant info:
mWakeLock = mPowerManager.newWakeLock(field, getLocalClassName());
where
field = PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK or 32
My proximity sensor code works incorrectly only on
1. Samsung GT-I9082 Android 4.2.2 (API 17) [duos grand]
2. Samsung SM-G925I Android 5.1.1 (API 22) [s6 edge]
the code:
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
...new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
if (event.values[0] <= 5) { //Sleep
turnOffScreen();
} else { //Wake
turnOnScreen();
}...
// registering listener with SensorManager.SENSOR_DELAY_NORMAL);
the problem:
Logged values returned by proximity sensor on both devices fluctuate, even while we place a hand over the phones constantly for a few seconds; 10-15 without moving it.
Like:
Samsung 6 edge-
02-10 20:12:36.532: D/SensorManager(3467): Proximity, val = 8.0 [far]
02-10 20:12:36.532: D/SensorManager(29335): Proximity, val = 8.0 [far]
02-10 20:12:36.532: D/DisplayPowerController(3467): [sensor] setProximitySensorEnabled::unregisterListener
02-10 20:12:36.532: D/PowerManagerService(3467): [s] DisplayPowerCallbacks : onProximityNegative()
02-10 20:12:36.562: I/Sensors(3467): Proximity old sensor_state 33554560, new sensor_state : 33554432 en : 0
02-10 20:12:36.632: I/Sensors(3467): Proximity old sensor_state 33554944, new sensor_state : 33555072 en : 1
02-10 20:12:36.642: D/SensorManager(29335): registerListener :: 5, TMD4903 Proximity Sensor, 200000, 0,
02-10 20:12:36.652: D/SensorManager(29335): Proximity, val = 8.0 [far]
02-10 20:12:36.662: I/Sensors(3467): ProximitySensor - 8(cm)
02-10 20:12:36.672: D/SensorManager(29335): Proximity, val = 8.0 [far]
02-10 20:12:41.752: I/Sensors(3467): Proximity old sensor_state 33554560, new sensor_state : 33554432 en : 0
02-10 20:12:41.822: I/Sensors(3467): Proximity old sensor_state 33554432, new sensor_state : 33554560 en : 1
02-10 20:12:41.842: D/SensorManager(29335): registerListener :: 5, TMD4903 Proximity Sensor, 200000, 0,
02-10 20:12:41.842: D/SensorManager(29335): Proximity, val = 8.0 [far]
02-10 20:12:41.872: I/Sensors(3467): ProximitySensor - 8(cm)
02-10 20:12:41.872: D/SensorManager(29335): Proximity, val = 8.0 [far]
02-10 20:12:50.482: I/Sensors(3467): ProximitySensor - 0(cm)
02-10 20:12:50.482: D/SensorManager(29335): Proximity, val = 0.0 [close]
02-10 20:12:50.482: D/DisplayPowerController(3467): [sensor] setProximitySensorEnabled::registerListener
02-10 20:12:50.482: D/SensorManager(3467): registerListener :: 5, TMD4903 Proximity Sensor, 200000, 0,
02-10 20:12:50.482: D/SensorManager(3467): Proximity, val = 0.0 [close]
02-10 20:12:50.482: D/PowerManagerService(3467): [s] DisplayPowerCallbacks : onProximityPositive()
02-10 20:12:50.482: D/PowerManagerService(3467): [s] DisplayPowerCallbacks : onProximityPositive()
Samsung Duos: the fluctuation values differ in 0.0 and 5.0 unlike 8.0 above
What are there other ways or permutation-combinations which can be applied successfully and accurately to solve this issue?
Is it because of different sensors; GP2A Proximity Sensor and APDS-9930/QPDS-T930 Proximity & Light ?
have already spent quite some time on it..in vain
So turns out this is a registered issue with Samsung devices, here is a collection of pages which could help anybody facing this:
Use onWindowFocusChanged. This made sense to me and my implementation related to onPause() was successful on following the same.
Source and Explanation: activity-onpause-to-handle-focus
What helped me: instead of inonPause()
;@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if(!hasFocus){ clearProximityListeners(); } }
More reference:
From quite a different aspect, Bug on unregister...Otto bus.. quote: For us, this only appears on Samsung devices. And after a quick search,...
Please edit, correct, improve the answer if there's any more clarity to it.. hope it saves some time.
这篇关于Android 接近传感器问题仅在三星设备中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!