我在我的应用程序中使用Google健康工具包。我知道Health kit没有直接提供传感器步数。我读了google fit文档我发现我们可以在后台使用Recording api for Step Count。因此,如果可以使用Recording api和Sensor api在后台获取步骤计数,请告诉我如何实现这一点。我想了解用户活动以及用户在后台活动期间采取的步骤数。任何帮助将不胜感激。
根据google fit文档,如果我的应用程序订阅了记录数据类型,那么即使我的应用程序没有运行,它也会记录该类型的数据并将其存储到HISTORYAPI中。这是订阅代码
Fitness.RecordingApi.subscribe(fitnessClient, DataType.TYPE_ACTIVITY_SAMPLE)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status 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 activity !");
}
} else {
Log.e(TAG, "There was a problem subscribing.");
}
}
});
Fitness.RecordingApi.subscribe(fitnessClient,DataType.TYPE_STEP_COUNT_DELTA).
setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status arg0) {
if(arg0.isSuccess()){
Log.e("Steps Recording","Subcribe");
}
}
});
现在我订阅了步骤和活动。但直到现在它还没有感觉到什么。任何人都可以解释订阅记录数据类型的目的是什么。
答案 0 :(得分:0)
我只是用谷歌搜索google fit API,似乎传感器API用于从传感器获取数据,如用户的心率,Recording api用于收集数据,例如用户运行时的地理定位数据,历史api用于编辑记录数据。记录的数据是通过google fit在后台收集的,这些数据可以存储在云中,但是这些数据存储在本地设备中,或者这些数据是否存储在本地设备中?我没有看到任何关于它的信息,在你的代码中我也没有看到它。我没有使用google fit API做任何项目,对不起,不禁多了。
答案 1 :(得分:0)
我知道这是一个老问题,但我也需要帮助,所以我会尽力帮助:
您的活动需要实现此目的:
implements NavigationView.OnNavigationItemSelectedListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
在onCreate之前创建此变量:public GoogleApiClient mGoogleApiClient = null;
在onCreate里面写这个:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.HISTORY_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addConnectionCallbacks(this)
.enableAutoManage(this, 0, this)
.build();
不要忘记OAuth身份验证。
然后你需要这个方法:
public void onConnected(@Nullable Bundle bundle) {
Log.e("HistoryAPI", "onConnected");
}
@Override
public void onConnectionSuspended(int i) {
Log.e("HistoryAPI", "onConnectionSuspended");
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e("HistoryAPI", "onConnectionFailed");
}
public void onClick(View v) {
}
在此之后,您将创建此方法,阅读步骤计数器(Google Fit API计算它们)
public void displayStepDataForToday() {
DailyTotalResult result = Fitness.HistoryApi.readDailyTotal( mGoogleApiClient, DataType.TYPE_STEP_COUNT_DELTA ).await(1, TimeUnit.MINUTES);
showDataSet(result.getTotal());
}
这是displayStepDataForToday()
private void showDataSet(DataSet dataSet) {
Log.e("History", "Data returned for Data type: " + dataSet.getDataType().getName());
DateFormat dateFormat = DateFormat.getDateInstance();
DateFormat timeFormat = DateFormat.getTimeInstance();
for (DataPoint dp : dataSet.getDataPoints()) {
Log.e("History", "Data point:");
Log.e("History", "\tType: " + dp.getDataType().getName());
Log.e("History", "\tStart: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
Log.e("History", "\tEnd: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)) + " " + timeFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
for(Field field : dp.getDataType().getFields()) {
Log.e("History", "\tField: " + field.getName() +
" Value: " + dp.getValue(field));
//writeToFile(dp.getValue(field).asInt());
//this is how I save the data (wit the writeToFile)
}
}
}
最后,您需要创建一个类(在您的活动中)以使用displayStepDataForToday()
方法
public class ViewTodaysStepCountTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
displayStepDataForToday();
return null;
}
}
您只需要在需要时启动活动,然后获取其值。这在后台运行并根据需要更新。如果您希望此代码更快地更新,您可以进行一些研究,但我认为您需要更改此行:
public void displayStepDataForToday() {
DailyTotalResult result = Fitness.HistoryApi.readDailyTotal( mGoogleApiClient, DataType.TYPE_STEP_COUNT_DELTA ).await(1, TimeUnit.MINUTES);