我有以下代码用于从Google Fit检索用户活动列表:
public void getActivitiesData(Date from,Date till) { DataReadRequest readRequest = new DataReadRequest.Builder() .aggregate(DataType.TYPE_ACTIVITY_SEGMENT,DataType.AGGREGATE_ACTIVITY_SUMMARY) .bucketByTime(1,TimeUnit.DAYS) .setTimeRange(from.getTime(),till.getTime(),TimeUnit.MILLISECONDS) .build(); Fitness.HistoryApi.readData(apiClient,readRequest).setResultCallback(new com.google.android.gms.common.api.ResultCallback<DataReadResult>() { @Override public void onResult(DataReadResult dataReadResult) { Status status = dataReadResult.getStatus(); if (status.isSuccess()) { for (Bucket bucket : dataReadResult.getBuckets()) { if (!bucket.getDataSets().isEmpty()) { DataSet dataSet = bucket.getDataSets().get(0); String sourceAppPackageName = getSourceAppPackageNameFromDataSet(dataSet); for (DataPoint dp : dataSet.getDataPoints()) { for (Field field : dp.getDataType().getFields()) { String fieldName = field.getName(); if (fieldName != null && fieldName.equals("activity")) { String type = FitnessActivities.getValue(dp); Date from = new Date(dp.getStartTime(TimeUnit.MILLISECONDS)); Date till = new Date(dp.getEndTime(TimeUnit.MILLISECONDS)); // store retrieved values to the data object,omitted } } } } } } } }); } private static String getSourceAppPackageNameFromDataSet(DataSet dataSet) { String result = null; if (dataSet.getDataSource() != null) { result = dataSet.getDataSource().getAppPackageName(); } return result; }
要将活动插入Google Fit,我使用了Google健身应用和Runkeeper(目前,这些应用似乎只是与Fit集成的应用).
我的代码按预期检索这些活动,但是,对于每个活动,我的getSourceAppPackageNameFromDataSet()方法返回“com.google.android.gms”作为包名.根据Data Attribution section in Google Fit documentation,我希望该方法返回Runkeeper或Google Fit的包名,但这不会发生.
解决方法
DataPoint.getOriginalDataSource().getAppPackageName()将完成这一操作.它为我的Withings比例返回com.withings.wiscale2,而DataSet.getDataSource().getAppPackageName()总是返回com.google.android.gms.
这里有一个类似的问题:DataSource.getAppPackageName() always returns “com.google.android.gms” in Google Fit