在Android Lollipop上找不到Sensor.TYPE_STEP_DETECTOR [英] Sensor.TYPE_STEP_DETECTOR not found on android Lollipop
问题描述
我正在开发一个计步器android应用程序,为此,我使用了Sensor.TYPE_STEP_DETECTOR(可从android kitkat获得). 在Nexus 5和Samsung Alpha上一切正常,但随后我在Moto G(棒棒糖)和Nexus 4(棒棒糖)上测试了我的应用, 当我尝试获取Sensor.TYPE_STEP_DETECTOR类型的传感器时,两个设备都返回null.
I am developing a pedometer android application and for that i used Sensor.TYPE_STEP_DETECTOR which is available from android KitKat. Everything was working fine at Nexus 5 and Samsung Alpha but then i tested my application on Moto G(Lollipop) and Nexus 4(Lollipop), both the device are returning null when i am trying to get sensor of Sensor.TYPE_STEP_DETECTOR type.
这是我的代码:
private boolean checkSensorAvailability() {
SensorManager sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
if(sensor==null){return false;}
return true;
}
据我所知,此传感器需要设备中的加速度计. 两种设备都可用.
As per my knowledge this sensor required Accelerometer in Device. Which is available in both devices.
请帮助我解决此问题.
谢谢
推荐答案
某些设备只是拒绝了该功能(完全唤醒).
Some devices simply refused that (full wake-up) functionality.
问题是功耗.如果手机每执行一次步骤就唤醒应用处理器,它将消耗更多电量,并可能导致电池寿命变短.对于您的手机,制造商已决定不支持传感器的唤醒"版本,该传感器会在检测到脚步时打开手机.
The problem is power consumption. If the phone wakes up the application processor every time a step occurred, it will draw more power and could lead to poor battery life. With your phone, the manufacturer has made a decision NOT to support a "wake-up" version of the sensor that would turn the phone on when a step is detected.
https://stackoverflow.com/a/28071354/603270
作为一种解决方法,我建议查看 https://github.com/j4velin/Pedometer/blob/master/src/main/java/de/j4velin/pedometer/SensorListener.java
As a workaround, I recommend looking at https://github.com/j4velin/Pedometer/blob/master/src/main/java/de/j4velin/pedometer/SensorListener.java
public void onSensorChanged(final SensorEvent event) {
steps = (int) event.values[0];
// ...
}
这篇关于在Android Lollipop上找不到Sensor.TYPE_STEP_DETECTOR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!