简体   繁体   English

Android SensorManager vs Google Fit 计步

[英]Android SensorManager vs Google Fit for step count

I've been researching how to read step count in my Android app, and found two completely different ways to do it in the documentation:我一直在研究如何在我的 Android 应用程序中读取步数,并在文档中找到了两种完全不同的方法:

Using SensorManager : 使用传感器管理器

val sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)

Using Google Fit API : 使用 Google Fit API

private class VerifyDataTask extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... params) {

        long total = 0;

        PendingResult<DailyTotalResult> result = Fitness.HistoryApi.readDailyTotal(mClient, DataType.TYPE_STEP_COUNT_DELTA);
        DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
        if (totalResult.getStatus().isSuccess()) {
            DataSet totalSet = totalResult.getTotal();
            total = totalSet.isEmpty()
                    ? 0
                    : totalSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
        } else {
            Log.w(TAG, "There was a problem getting the step count.");
        }

        Log.i(TAG, "Total steps: " + total);

        return null;
    }
}

There is no indication as to the difference between these two approaches, does anybody know:没有迹象表明这两种方法之间的区别,有人知道:

  • Do they get step count data from the same source, ie will they give the same numbers?他们是否从同一来源获得步数数据,即他们会给出相同的数字吗?
  • Does the Google Fit terms and conditions apply to using SensorManager data? Google Fit 条款和条件是否适用于使用 SensorManager 数据?
  • Do both retrieve data from Wear OS if the user is wearing one?如果用户佩戴,两者都从 Wear OS 检索数据吗?
  • Why are there two approaches, what's the difference?为什么有两种方法,有什么区别?

Similar question about heart rate here , but with no answers. 关于心率的类似问题在这里,但没有答案。 Any guidance much appreciated!非常感谢任何指导!

On a high level:在高层次上:

SensorManager gives you direct access to the sensors that can be used to record (amongst other things) step count. SensorManager可让您直接访问可用于记录(除其他外)步数的传感器。 This gives you raw, unprocessed data in real-time.这会实时为您提供原始的、未经处理的数据。

Google Fit uses the sensors on your device(s) to collect (amongst other things) step count. Google Fit 使用您设备上的传感器来收集(除其他外)步数。 They also (might?) do some magic to improve this data.他们也(可能?)做一些魔法来改善这些数据。 And make sure that if you go out for a run with both your phone and watch it only logs it once.并确保如果您带着手机出去跑步并且观看它只记录一次。 Google Fit gives you access to historical data. Google 健身可让您访问历史数据。

To answer your questions:回答您的问题:

  • Do they get step count data from the same source, ie will they give the same numbers?他们是否从同一来源获得步数数据,即他们会给出相同的数字吗?

Yes, they get the data from the same source.是的,他们从同一来源获取数据。 However, in Google Fit you have access to data from all your connected devices.但是,在 Google 健身中,您可以访问所有已连接设备的数据。 From the SensorManager you only have access to the data on that specific device.SensorManager您只能访问该特定设备上的数据。 They will not necessarily give the exact same number.他们不一定会给出完全相同的数字。

  • Does the Google Fit terms and conditions apply to using SensorManager data? Google Fit 条款和条件是否适用于使用 SensorManager 数据?

No, the SensorManager doesn't come with any specific terms and conditions.不, SensorManager没有任何特定的条款和条件。 It's part of the Android platform and can be used just like any other feature therein.它是 Android 平台的一部分,可以像其中的任何其他功能一样使用。

  • Do both retrieve data from Wear OS if the user is wearing one?如果用户佩戴,两者都从 Wear OS 检索数据吗?

As mentioned above the SensorManager only records data on the device where the code is running.如上所述, SensorManager仅记录运行代码的设备上的数据。 Google Fit is able to collect data from a Wear OS device, if it's configured to do so by the user. Google Fit 能够从 Wear OS 设备收集数据,前提是用户对其进行了配置。

  • Why are there two approaches, what's the difference?为什么有两种方法,有什么区别?

I've already explained some of the key differences above.我已经解释了上面的一些主要区别。 To answer the "why": Different application behaviors requires different solutions.回答“为什么”:不同的应用程序行为需要不同的解决方案。 Sometimes the SensorManager is the best alternative, and sometimes Google Fit is.有时SensorManager是最好的选择,有时 Google Fit 是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM