问题
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.
Here is my code:
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.
Please help me to resolve this issue.
Thanks
回答1:
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
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];
// ...
}
回答2:
As the Android documentation (https://developer.android.com/about/versions/android-4.4.html) states:
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.
Hence Nexus 4 and Moto G2 won't support it. You should check for other devices.
回答3:
Not every device will support steps sensors:
From Docs:
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.
Found here: https://developer.android.com/about/versions/android-4.4.html
来源:https://stackoverflow.com/questions/30891730/sensor-type-step-detector-not-found-on-android-lollipop