gpt4 book ai didi

java - 如何从 Android 中的 Google Fit 服务检索健康数据

转载 作者:行者123 更新时间:2023-11-30 05:18:36 25 4
gpt4 key购买 nike

我正在编写一个 Android 程序来检索特定类型的健康数据。我阅读了谷歌文档以从用户那里检索数据,但最终无法检索。任何人都可以通过最简单的java方式帮助解决这个问题。我当前的代码附在这里。

public class MainActivity extends AppCompatActivity {

int GOOGLE_FIT_PERMISSIONS_REQUEST_CODE = 156;
GoogleSignInAccount googleSignInAccount=null;

//Request Data for STEP_COUNT_DELTA with READ permissions
//FitnessOptions - API
FitnessOptions fitnessOptions = FitnessOptions.builder()
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
.build();

@Override
protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);




//Get instance of Google Account
googleSignInAccount = googleSignInAccount(fitnessOptions);


if (!GoogleSignIn.hasPermissions(googleSignInAccount, fitnessOptions)) {
GoogleSignIn.requestPermissions(
this, // your activity
GOOGLE_FIT_PERMISSIONS_REQUEST_CODE, // e.g. 1
googleSignInAccount,
fitnessOptions);
} else {

accessGoogleFit();

}

}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == GOOGLE_FIT_PERMISSIONS_REQUEST_CODE) {

accessGoogleFit();
}
}
}

private void accessGoogleFit() {

Calendar cal = Calendar.getInstance();
Date date = new Date();
cal.setTime(date);

long endTime = cal.getTimeInMillis();
cal.add(Calendar.WEEK_OF_YEAR, -1);
long startTime = cal.getTimeInMillis();

//SessionReadRequest sessionReadRequest = new SessionReadRequest.Builder().read(DataType.AGGREGATE_ACTIVITY_SUMMARY).setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS).build();

DataReadRequest dataReadRequest = new DataReadRequest.Builder().read(DataType.AGGREGATE_ACTIVITY_SUMMARY).setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS).build();

Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this)).readData(dataReadRequest)
.addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {
@Override
public void onSuccess(DataReadResponse dataReadResponse) {

List<DataSet> dataSets = dataReadResponse.getDataSets();
for(DataSet dataSet: dataSets){
for(Field field: dataSet.getDataType().getFields()){
if(field.getName().equalsIgnoreCase(FitnessActivities.SLEEP)){
Toast.makeText(MainActivity.this, "ENCONTRADO", Toast.LENGTH_LONG).show();
}
}
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {

}
});

}





public GoogleSignInAccount googleSignInAccount(FitnessOptions fitnessOptions) {
GoogleSignInAccount account = GoogleSignIn.getAccountForExtension(this, fitnessOptions);
return account;
}
}

通过此代码,在访问 google fit -api 之前向用户存档 google 身份验证,所有 api 访问均在 accessGooogleFit() 函数中实现

最佳答案

此代码 fragment 来自 Google API 文档

SessionReadRequest request = new SessionReadRequest.Builder()
.readSessionsFromAllApps()
// Activity segment data is required for details of the fine-
// granularity sleep, if it is present.
.read(DataType.TYPE_ACTIVITY_SEGMENT)
.setTimeInterval(1576690819, 1576750401, TimeUnit.SECONDS)
.build();

Task<SessionReadResponse> task = sessionClient.readSession(request);

task.addOnSuccessListener(response -> {
// Filter the resulting list of sessions to just those that are sleep.
List<Session> sleepSessions = response.getSessions().stream()
.filter(s -> s.getActivity().equals(FitnessActivities.SLEEP))
.collect(Collectors.toList());

for (Session session : sleepSessions) {
Log.d("AppName", String.format("Sleep between %d and %d",
session.getStartTime(TimeUnit.SECONDS),
session.getEndTime(TimeUnit.SECONDS)));

// If the sleep session has finer granularity sub-components, extract them:
List<DataSet> dataSets = response.getDataSet(session);
for (DataSet dataSet : dataSets) {
for (DataPoint point : dataSet.getDataPoints()) {
// The Activity defines whether this segment is light, deep, REM or awake.
String sleepStage = point.getValue(Field.FIELD_ACTIVITY).asActivity();
long start = point.getStartTime(TimeUnit.SECONDS);
long end = point.getEndTime(TimeUnit.SECONDS);
Log.d("AppName",
String.format("\t* %s between %d and %d", sleepStage, start, end));
}
}
}
});

关于java - 如何从 Android 中的 Google Fit 服务检索健康数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59925407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com