[软件设计/软件工程] 如何使用 Google Fit API 查找适用于 Android 的步骤?

[复制链接]
发表于 2022-5-6 15:57:44
我对此进行了几天的研究。我只需要应用程序中的一个简单的 TextView 区域来显示今天的步骤。

我已经设法使用下面的代码进行身份验证。它弹出请求许可,并认为我选择了正确的。

但我不知道如何简单地获取步数信息。我希望这只是几行代码。任何帮助将不胜感激。谢谢

编辑1:我只需要获取步数。我可以想办法稍后展示它。我还有一个祝酒词来帮助我弄清楚发生了什么。
  1. private void buildFitnessClient() {
  2.     if (mClient == null) {
  3.         mClient = new GoogleApiClient.Builder(this)
  4.                 .addApi(Fitness.SENSORS_API)
  5.                 .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
  6.                 .addConnectionCallbacks(
  7.                         new GoogleApiClient.ConnectionCallbacks() {
  8.                             @Override
  9.                             public void onConnected(Bundle bundle) {
  10.                                 Log.i(TAG, "Connected!!!");
  11.                                 // Now you can make calls to the Fitness APIs.
  12.                                 Toast.makeText(getBaseContext(), "Connected!", Toast.LENGTH_LONG).show();


  13.                             }

  14.                             @Override
  15.                             public void onConnectionSuspended(int i) {
  16.                                 // If your connection to the sensor gets lost at some point,
  17.                                 // you'll be able to determine the reason and react to it here.
  18.                                 if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
  19.                                     Log.i(TAG, "Connection lost.  Cause: Network Lost.");
  20.                                     Toast.makeText(getBaseContext(), "Connection lost.  Cause: Network Lost.", Toast.LENGTH_LONG).show();

  21.                                 } else if (i
  22.                                         == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
  23.                                     Log.i(TAG,
  24.                                             "Connection lost.  Reason: Service Disconnected");
  25.                                 }
  26.                             }
  27.                         }
  28.                 )
  29.                 .enableAutoManage(this, 0, new GoogleApiClient.OnConnectionFailedListener() {
  30.                     @Override
  31.                     public void onConnectionFailed(ConnectionResult result) {
  32.                         Log.i(TAG, "Google Play services connection failed. Cause: " +
  33.                                 result.toString());
  34.                         Toast.makeText(getBaseContext(), "Google Play services connection failed. Cause: " +
  35.                                 result.toString(), Toast.LENGTH_LONG).show();

  36.                     }
  37.                 })
  38.                 .build();
  39.     }
  40. }
复制代码

回答
查看 Google 的官方文档,了解如何从 Fit 读取数据:
  1. // Setting a start and end date using a range of 1 week before this moment.
  2. Calendar cal = Calendar.getInstance();
  3. Date now = new Date();
  4. cal.setTime(now);
  5. long endTime = cal.getTimeInMillis();
  6. cal.add(Calendar.WEEK_OF_YEAR, -1);
  7. long startTime = cal.getTimeInMillis();

  8. java.text.DateFormat dateFormat = getDateInstance();
  9. Log.i(TAG, "Range Start: " + dateFormat.format(startTime));
  10. Log.i(TAG, "Range End: " + dateFormat.format(endTime));

  11. DataReadRequest readRequest = new DataReadRequest.Builder()
  12.         // The data request can specify multiple data types to return, effectively
  13.         // combining multiple data queries into one call.
  14.         // In this example, it's very unlikely that the request is for several hundred
  15.         // datapoints each consisting of a few steps and a timestamp.  The more likely
  16.         // scenario is wanting to see how many steps were walked per day, for 7 days.
  17.         .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
  18.         // Analogous to a "Group By" in SQL, defines how data should be aggregated.
  19.         // bucketByTime allows for a time span, whereas bucketBySession would allow
  20.         // bucketing by "sessions", which would need to be defined in code.
  21.         .bucketByTime(1, TimeUnit.DAYS)
  22.         .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
  23.         .build();
复制代码

GitHub 上的 History API 示例应用程序:

查看 GitHub 上的示例项目。





上一篇:如何使用 openssl 创建公钥和私钥?
下一篇:使用resolve eject将promise转换为异步函数

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表