如何从活动识别中获取步骤(将SensorRecognitionApi与SensorEventListener结合使用)

时间:2017-03-22 15:01:41

标签: android sensormanager activity-recognition

我正在使用适用于Android的Google ActivityRecognitionApi来了解用户何时行走,跑步或骑自行车。 有没有办法在执行每项活动时获取用户执行的步骤?我正在使用IntentService在后台进行活动识别。我还需要在后台进行操作。

1 个答案:

答案 0 :(得分:-1)

您需要使用StepCounter传感器来获取这些值。

public class StepCounterService extends Service implements SensorEventListener{

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    //Check if the stepCounter is available first.
    List<Sensor> gravSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
    for(Sensor each : gravSensors){
    //check for sensor named stepcounter in the list.
            Log.d("Sesnor_list",each.getName());
    }
    //If it's available we can retrieve the value using following code
    Sensor sensor;

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    sensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
    sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    return START_STICKY;
}
 //An onSensorChanged event gets triggered every time new step count is detected.
  @Override
public void onSensorChanged(SensorEvent event) {

    if(event.sensor.getType() == sensor.TYPE_STEP_COUNTER){
        Log.d("step_count = ",event.values[0]);
    }

}
}