从 Google 获取电流 Activity 适合 API Android

Get Current Activity from Google Fit API Android

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

请提供solution/feedback。

代码:

@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()
等于 "biking"、"walking"、"in_vehicle" 等

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

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

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

您可以在此处找到我创建的示例项目。

https://github.com/cyfung/ActivityRecognitionSample

重要说明:您可能无法像您请求的那样频繁地获取数据!

Beginning in API 21, activities may be received less frequently than the detectionIntervalMillis parameter if the device is in power save mode and the screen is off.

关键组件:

onCreate

中创建GoogleApi客户端
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");
  }

开始activity识别(不应在GoogleApi连接前调用)。使用 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,您还需要在清单中添加以下内容。

<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>

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

为了将数据取回 activity 我使用了在 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);
  }

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

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

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