Reputation: 21
I'm developing an application which uses the TYPE_STEP_DETECTOR. I want to detect whether user is walking even when the screen of the phone is off. But the sensor TYPE_STEP_DETECTOR does not work when the screen is off. So I use PowerManager/WakeLock to make the CPU not hibernate. But it STILL not work. As a comparison, I add a magnetic sensor listener and the step detector listener to my demo app, when screen on, both of them can recieve data, but when the screen is off, only the magnetic sensor can recieve data.
PowerManager manager = (PowerManager)getSystemService(Context.POWER_SERVICE);
mWakeLock = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
mWakeLock.acquire();
Then:
stepDetectorSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
sensorManager.registerListener(stepDetectorListener, stepDetectorSensor, SensorManager.SENSOR_DELAY_NORMAL);
Then:
magnetSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sensorManager.registerListener(magnetSensorListener, magnetSensor, SensorManager.SENSOR_DELAY_NORMAL);
And:
stepDetectorListener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
public void onSensorChanged(SensorEvent arg0) {
//NO DATA when screen off
}
};
magnetSensorListener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
public void onSensorChanged(SensorEvent arg0) {
//DATA when screen off
}
};
Upvotes: 1
Views: 839
Reputation: 20944
If you want to check if user is walking - you got to take completely different approach. Google released an ActivityRecognitionApi
. This framework allows you to track user's current activity (such as WALKING, RUNNING and others).
It also allows you to track activity when app is in background.
Upvotes: 0