从Google Fit API Android获取当前活动

从Google Fit API Android获取当前活动,android,google-fit,google-fit-sdk,Android,Google Fit,Google Fit Sdk,我正在开发演示应用程序,以便使用GoogleFit获取当前的活动示例。我能准确地获得速度和距离。但它并不是经常回到“车内”或“骑自行车”的状态,尽管我也处于同样的状态。找到相同的附加屏幕截图。我获得了59.40KM/H(36.91 M/H)的速度,当时它没有返回“in_vehicle”活动状态 请提供解决方案/反馈 代码: @Override public void onDataPoint(DataPoint dataPoint) { for (Field field : dataP

我正在开发演示应用程序,以便使用GoogleFit获取当前的活动示例。我能准确地获得速度和距离。但它并不是经常回到“车内”或“骑自行车”的状态,尽管我也处于同样的状态。找到相同的附加屏幕截图。我获得了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";
                        }
                    }
            }
}
谢谢


我不熟悉google fit api,所以我能给你的唯一建议就是仔细检查你的代码。是
Integer.parseInt(val.toString())

返回正确的int和can
FitnessActivities.getName()

平等的“骑自行车”、“步行”、“在车内”等

从这里我可以看到:

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


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

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

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

从API 21开始,接收活动的频率可能低于 如果设备处于节能状态,则检测间隔为毫秒参数 模式,屏幕关闭

关键组成部分:

onCreate

mGoogleApiClient =
        new GoogleApiClient.Builder(this).addApi(ActivityRecognition.API)
            .addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
按照Google api文档中的建议,在
onStart
onStop
中连接和断开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 connect之前调用)。使用
pendingent.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);
在清单中注册服务

    <service
            android:name=".ActivityDetectionService"
            android:exported="false">
    </service>
分别在
onResume
onPause
中注册和注销

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

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

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

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

这可能不是正确的答案,但会给您提供附近的状态结果。

我们最好理解一段代码。您应该编辑问题的标题和正文,以表明“活动”不是指“android.app.activity”,而是指“健身活动”字符串。您是否会发布有关如何触发Google API发送此
DataPoint
对象的代码?在如何使用
ActivityRecognitionApi
方面,您这样做的方式似乎与我所知道的相去甚远。您想要一个示例项目吗?@DerekFung:是的,示例项目会有所帮助。请做必要的事情。您能记录
log.i(标记,“检测到的数据点字段:”+field.getName())吗
Log.i(标记,“检测到的数据点值:”+val)内部
onDataPoint
和更新值以供我们查看?还要检查BasicSampleAPI示例项目,看看您是否遵循了所有步骤。这是非常不准确的。上面写着我走路的时候在车里/骑自行车。对于这个,我真的没办法,你可以查一下密码,我只是使用api这就是我提出的问题,我在Google Fit api中面临的问题。我在这里提供了api应该如何实现的代码,以排除错误实现api的问题。请问您是否真的在征求编程相关的建议?
mBroadcastReceiver = new BroadcastReceiver() {

  @Override
  public void onReceive(Context context, Intent intent) {
    ...
  }
}
  @Override
  protected void onResume() {
    super.onResume();
    registerReceiver(mBroadcastReceiver, newBroadcastIntentFilter());
  }

  @Override
  protected void onPause() {
    super.onPause();
    unregisterReceiver(mBroadcastReceiver);
  }
if (strState.equals("Automotive") && speed == 0.00)
{
   strState = "Not Moving";
}
else if (strState.equals("Not Moving") && speed > 5)
{
   strState = "Automotive";
}
else
{
   strState = "strState";
}