如何从 activity 识别中获取步骤(结合 ActivityRecognitionApi 和 SensorEventListener)

How to get the steps from activity recognition (Combine ActivityRecognitionApi with SensorEventListener)

我正在使用 Google ActivityRecognitionApi Android 来了解用户何时在步行、运行 或骑自行车。 有没有办法获取用户在练习每项活动时执行的步骤?我正在使用 IntentService 在后台进行 activity 识别。我还需要在后台获取步骤。

您将需要使用 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]);
    }

}
}