android - googleFit 获取每小时计步器更新回调?
问题描述
我将 googleFit 集成到 android 应用程序中。我正在使用历史 API 来按需获取更新,并订阅更新。这是我添加的代码片段。我正在使用初始化 google fit 并获取计步器。我需要从计步器挂起的单位时间回调以发送到服务器。
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SESSIONS_API)
.addApi(Fitness.HISTORY_API)
.addApi(Fitness.RECORDING_API)
.addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE))
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addConnectionCallbacks(this)
.enableAutoManage(this, 0, this)
.build();
public void subscribe() {
// To create a subscription, invoke the Recording API. As soon as the subscription is
// active, fitness data will start recording.
Fitness.RecordingApi.subscribe(mGoogleApiClient, DataType.TYPE_STEP_COUNT_CUMULATIVE)
.setResultCallback(status -> {
if (status.isSuccess()) {
if (status.getStatusCode()
== FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
Log.e(TAG, "Existing subscription for activity detected.");
} else {
Log.e(TAG, "Successfully subscribed!");
}
} else {
Log.w(TAG, "There was a problem subscribing.");
}
});
}
public void unSubscribe() {
// To create a subscription, invoke the Recording API. As soon as the subscription is
// active, fitness data will start recording.
Fitness.RecordingApi.unsubscribe(mGoogleApiClient, DataType.TYPE_STEP_COUNT_CUMULATIVE)
.setResultCallback(status -> {
if (status.isSuccess()) {
if (status.getStatusCode()
== FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
Log.e(TAG, "Existing subscription for activity detected.");
} else {
Log.e(TAG, "Successfully subscribed!");
}
} else {
Log.w(TAG, "There was a problem subscribing.");
}
});
}
long total = 0;
PendingResult<DailyTotalResult> result = Fitness.HistoryApi.readDailyTotal(mGoogleApiClient, DataType.TYPE_STEP_COUNT_DELTA);
DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
if (totalResult.getStatus().isSuccess()) {
DataSet totalSet = totalResult.getTotal();
total = totalSet.isEmpty()
? 0
: totalSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
} else {
Log.e(TAG, "There was a problem getting the step count.");
}
Log.e(TAG, "TOTAL STEPS : " + total);
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
Log.e(TAG, "onConnection DisConnected");
}
解决方案
推荐阅读
- docker - 从在线教程创建我的第一个 Docker 映像时,我收到此错误
- r - 读取大型 csv 文件并加入 r
- memory-leaks - 当我重新启动我的电脑时,堆内存会被释放吗?
- django - 扩展组模型的嵌套序列化程序在用户序列化程序中缺少某些字段
- javascript - 停止在三个 JS 中长距离加载对象
- python - 根据某些条件从字典的python字典中删除键
- linux - 两个列表之间的 grep
- python - Django 模板中的日期和时间
- javascript - 地图条目存储在哪里?
- powershell - 如何将字符串连接到powershell中嵌套数组内的值?