Sensor.TYPE_STEP_DETECTOR 在 android 棒棒糖上找不到

Sensor.TYPE_STEP_DETECTOR not found on android Lollipop

我正在开发计步器 android 应用程序,为此我使用了 Sensor.TYPE_STEP_DETECTOR,可从 android KitKat 获得。 在 Nexus 5 和 Samsung Alpha 上一切正常,但后来我在 Moto G(Lollipop) 和 Nexus 4(Lollipop) 上测试了我的应用程序, 当我尝试获取 Sensor.TYPE_STEP_DETECTOR 类型的传感器时,两个设备都返回 null。

这是我的代码:

 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;
}

据我所知,此传感器需要设备中的加速度计。 这两种设备都可用。

请帮我解决这个问题。

谢谢

有些设备只是拒绝该(完全唤醒)功能。

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://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 文档 (https://developer.android.com/about/versions/android-4.4.html) 所述:

两个步进传感器都依赖于硬件(Nexus 5 是第一个支持它们的设备),因此您应该使用 FEATURE_SENSOR_STEP_DETECTOR 和 FEATURE_SENSOR_STEP_COUNTER 常量检查 hasSystemFeature() 的可用性。

因此 Nexus 4 和 Moto G2 不支持它。您应该检查其他设备。

并非所有设备都支持计步传感器:

来自文档:

Both step sensors are hardware dependent (Nexus 5 is the first device to support them), so you should check for availability with hasSystemFeature(), using the FEATURE_SENSOR_STEP_DETECTOR and FEATURE_SENSOR_STEP_COUNTER constants.

在此处找到: https://developer.android.com/about/versions/android-4.4.html