【问题标题】:Get Current Activity from Google Fit API Android从 Google Fit API Android 获取当前活动
【发布时间】:2015-11-29 03:51:19
【问题描述】:

我正在开发演示应用程序以使用 Google Fit 获取当前活动示例。我可以正确获得速度和距离。但是,尽管我处于同一状态,但它并没有非常频繁地返回“in_vehicle”或“biking”状态。找到相同的附加屏幕截图。我得到了 59.40KM/H(36.91 M/h) 的速度,当时它没有返回“in_vehicle”活动状态。

请提供相同的解决方案/反馈。

代码:

@Override
 public void onDataPoint(DataPoint dataPoint) {
     for (Field field : dataPoint.getDataType().getFields()) {
        Value val = dataPoint.getValue(field);
           if(field.getName().trim().toLowerCase().equals("activity"))
                    {
                        if(FitnessActivities.getName(Integer.parseInt(val.toString())).equals("biking"))
                        {
                            strState = "Cycling";
                        }
                        else if(FitnessActivities.getName(Integer.parseInt(val.toString())).equals("in_vehicle"))
                        {
                            strState = "Automotive";
                        }
                        else if(FitnessActivities.getName(Integer.parseInt(val.toString())).equals("walking"))
                        {
                            strState = "Walking";
                        }
                        else
                        {
                            strState = "Not Moving";
                        }
                    }
            }
}

谢谢。

【问题讨论】:

  • 一段代码我们会更好理解吗。
  • 您应该编辑问题的标题和正文,以表明“活动”不是指“android.app.Activity”,而是指“健身活动”字符串。
  • 您能否发布有关如何触发 Google API 发送此 DataPoint 对象的代码?你这样做的方式似乎与我所知道的应该如何使用ActivityRecognitionApi 相去甚远。您想要一个示例项目吗?
  • @DerekFung :是的,示例项目会有所帮助。请做需要。
  • 您可以在onDataPoint 中记录Log.i(TAG, "Detected DataPoint field: " + field.getName());Log.i(TAG, "Detected DataPoint value: " + val); 并更新值供我们查看吗?另请检查 BasicSampleAPI 示例项目 github.com/googlesamples/android-fit/tree/master/… 以查看您是否已遵循所有步骤

标签: android google-fit google-fit-sdk


【解决方案1】:

你可以在这里找到我创建的示例项目。

https://github.com/cyfung/ActivityRecognitionSample

重要提示:您可能无法按要求频繁获取数据!

从 API 21 开始,接收活动的频率可能低于 如果设备处于省电模式,则为 detectionIntervalMillis 参数 模式,屏幕关闭。

关键组件:

onCreate中创建GoogleApiClient

mGoogleApiClient =
        new GoogleApiClient.Builder(this).addApi(ActivityRecognition.API)
            .addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();

按照 Google Api 文档中的建议,在 onStartonStop 中连接和断开 api 客户端。

  @Override
  protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
    mStatusView.setText("connecting");
  }

  @Override
  protected void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
    mStatusView.setText("disconnected");
  }

启动活动识别(不应在连接 Google Api 之前调用)。使用PendingIntent.getService 创建待处理的意图作为回调。

final PendingResult<Status>
    statusPendingResult =
    ActivityRecognition.ActivityRecognitionApi
        .requestActivityUpdates(mGoogleApiClient, DETECT_INTERVAL, PendingIntent
            .getService(this, 0, new Intent(this, ActivityDetectionService.class),
                          PendingIntent.FLAG_UPDATE_CURRENT));
statusPendingResult.setResultCallback(this);

IntentService 是建议回调的标准方法

public class ActivityDetectionService extends IntentService {

  protected static final String TAG = "activityDetectionService";

  public ActivityDetectionService() {
    super(TAG);
  }

  @Override
  protected void onHandleIntent(Intent intent) {
    final ActivityRecognitionResult
        activityRecognitionResult =
        ActivityRecognitionResult.extractResult(intent);
    if (activityRecognitionResult == null) {
      return;
    }

    //process the result here, pass the data needed to the broadcast
    // e.g. you may want to use activityRecognitionResult.getMostProbableActivity(); instead
    final List<DetectedActivity>
        probableActivities =
        activityRecognitionResult.getProbableActivities();

    sendBroadcast(MainActivity.newBroadcastIntent(probableActivities));
  }
}

在清单中注册服务。

    <service
            android:name=".ActivityDetectionService"
            android:exported="false">
    </service>

要使用 API,您还需要在清单中添加以下内容。

&lt;uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/&gt;

<meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />

为了将数据返回到活动中,我使用了在 onCreate 中创建的 BroadcastReceiver

mBroadcastReceiver = new BroadcastReceiver() {

  @Override
  public void onReceive(Context context, Intent intent) {
    ...
  }
}

分别在onResumeonPause注册和注销。

  @Override
  protected void onResume() {
    super.onResume();
    registerReceiver(mBroadcastReceiver, newBroadcastIntentFilter());
  }

  @Override
  protected void onPause() {
    super.onPause();
    unregisterReceiver(mBroadcastReceiver);
  }

【讨论】:

  • 非常不准确。当我走路时,它会显示 in_vehicle/biking。
  • 这个,实在是帮不上忙,你可以查一下代码,我就是用的api
  • 这就是我提出的问题,我正在使用 Google Fit api。
  • 我在这里提供API应该如何实现的代码,以排除错误实现API的问题。
  • 请问您是否真的在寻求与编程相关的建议?
【解决方案2】:

正如你所说,你的速度是正确的。你可以把自定义代码写在下面。

if (strState.equals("Automotive") && speed == 0.00)
{
   strState = "Not Moving";
}
else if (strState.equals("Not Moving") && speed > 5)
{
   strState = "Automotive";
}
else
{
   strState = "strState";
}

这可能不是正确的,但它会给你附近的状态结果。

【讨论】:

    【解决方案3】:

    我不熟悉 google fit api,所以我能给你的唯一建议是仔细检查你的代码。是
    Integer.parseInt(val.toString())
    返回正确的 int 并且可以
    FitnessActivities.getName()
    等于“骑自行车”、“步行”、“in_vehicle”等。

    我可以从这里看到:https://developers.google.com/fit/rest/v1/reference/activity-types

    自行车、车内和步行分别为 0、1 和 7。 例如,检查 FitnessActivities.getName(0) 返回的内容,还检查 val 是否返回不同的值,或者每次都返回相同的值。

    如果您的代码有任何问题,您应该知道代码在任何一行都在做什么,返回了哪些方法和函数...同时通知人们,以便他们更容易找到解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多