0

我正在尝试 BasicHistory Sample whis 存储数据然后读取它,但在我的情况下,代码卡在插入调用中。等待调用也不返回任何我尝试过使用异步方式的东西,这里是代码

com.google.android.gms.common.api.Status insertStatus =
Fitness.HistoryApi.insert(mClient, insertRequest).await(1, TimeUnit.MINUTES);
4

2 回答 2

1

您是否请求了正确的权限?

根据该 Google Fit 示例,它只需要“活动”的权限。如果您将该示例中使用的数据类型更改为其他不同的数据类型,请确保您设置了正确的权限。阅读Google Fit Doc中的授权部分了解更多信息。

您可以添加多个权限,只需向客户端添加新范围:

    // Create the Google API Client
    mClient = new GoogleApiClient.Builder(this)
            .addApi(Fitness.API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
            .addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE))
            .addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
    ...
于 2014-11-16T16:12:31.867 回答
0

要将 DataType 插入到 google fit 中,您必须创建一个相同类型的 DataSet,并确保您使用适当的范围构建 GoogleApiClient,就像上面所说的 jose 一样。以下是在 google fit 中插入 Height 的示例

public boolean saveUserHeight(int heightCentimiters) {
    // to post data
    float height = ((float) heightCentimiters) / 100.0f;
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    long endTime = cal.getTimeInMillis();
    cal.add(Calendar.DAY_OF_YEAR, -1);
    long startTime = cal.getTimeInMillis();
    DataSet heightDataSet = createDataForRequest(DataType.TYPE_HEIGHT, // for
            DataSource.TYPE_RAW, height, // weight in kgs
            startTime, // start time
            endTime, // end time
            TimeUnit.MILLISECONDS // Time Unit, for example,
                                    // TimeUnit.MILLISECONDS
    );
    com.google.android.gms.common.api.Status heightInsertStatus = Fitness.HistoryApi
            .insertData(fitnessClient, heightDataSet).await(1,
                    TimeUnit.MINUTES);
    if (heightInsertStatus.isSuccess()) {
        //Log.e("Height", heightCentimiters+"Inserted");
    } else {
        //Log.e("Height", "inserted failed");
    }
    return heightInsertStatus.isSuccess();
}
于 2016-05-27T08:49:20.267 回答