如何从 Google Fit API 获取步骤? [英] How do I get the steps from Google Fit API?

查看:30
本文介绍了如何从 Google Fit API 获取步骤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我对 Android 和 Google API 完全陌生.我有以下连接到 GoogleFit 的代码.我还有一个 API 密钥和 Oauth.

Pardon noobiness, I am completely new to Android, and Google APIs. I have the following code that connects to GoogleFit. I also have an API key and Oauth.

我在哪里/如何使用 API 密钥和 Oauth?关于如何获取它们的指南很多,但关于将它们放在何处/如何在应用中使用它们的信息为零.

Where/how do i use API key and Oauth? Lots of guides on how to obtain them but zero info on where to put them/how to use them in the app.

以及我如何实际使用返回的步骤.我设置了一个全局:

And how do I actually use the steps returned. I set up a global:

private int steps;

然后尝试通过以下方式设置:

and then try to set it via:

steps = (int)total;

但它什么都不做.

这是函数的其余部分.我如何真正从中获得步数.

Here is the rest of the function. How do I actually get the step count out of it.

 private void accessGoogleFit() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        long endtime = cal.getTimeInMillis();
        cal.add(Calendar.YEAR, -1);
        long starttime = cal.getTimeInMillis();

        DataReadRequest readRequest = new DataReadRequest.Builder()
                .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
                .bucketByTime(1, TimeUnit.DAYS)
                .setTimeRange(starttime, endtime, TimeUnit.MILLISECONDS)
                .build();

        Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
                .readDailyTotal(DataType.TYPE_STEP_COUNT_DELTA)
                .addOnSuccessListener(new OnSuccessListener<DataSet>() {
                    @Override
                    public void onSuccess(DataSet dataSet) {
                        Log.d("Status", "Success");
                        long total = dataSet.isEmpty()
                                ? 0
                                : dataSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
                        Log.d("Steps ", String.valueOf(total));
                        steps = (int)total; //Trying to get steps here
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d("Status", "Failure", e);
                    }
                })
                .addOnCompleteListener(new OnCompleteListener<DataSet>() {
                    @Override
                    public void onComplete(@NonNull Task<DataSet> task) {
                        Log.d("Status", "Complete");

                    }
                });

    }

我一直在阅读官方文档和 StackOverflow.但似乎 google 去年对 API 进行了重大更改,因此大多数内容已经过时,包括 google 自己的教程(2015 年发布).并且一些更新文档的地方提供了代码片段,我不知道如何使用它们或将它们放在哪里.

Ive been up and down the official documentation and StackOverflow. But it seems like google made big changes to the API last year and so most things are outdated, including google's own tutorials (posted in 2015). And a few places that have updated documentation provide snippets of code, and I have no idea how to use them or where to put them.

推荐答案

这就是我提出请求的方式Google Fit API:使用 FitnessOptions 请求,例如

This is how i make my request Google Fit API : request with FitnessOptions like

    FitnessOptions fitnessOptions = FitnessOptions.builder()
            .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .addDataType(DataType.TYPE_DISTANCE_DELTA, FitnessOptions.ACCESS_READ)
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
            .build();

您需要请求 GoogleSignIn.requestPermissions

you will need to request GoogleSignIn.requestPermissions

还有我的请求函数

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

        DataReadRequest readRequest = new DataReadRequest.Builder()
                .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
//                .read(DataType.TYPE_STEP_COUNT_DELTA)
                .bucketByTime(8, TimeUnit.DAYS)
                .enableServerQueries()
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build();

        Fitness.getHistoryClient(
                this,
                GoogleSignIn.getLastSignedInAccount(this))
                .readData(readRequest)
                .addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {
                    @Override
                    public void onSuccess(DataReadResponse dataReadResponse) {
                        Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.toString());
                        Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getStatus());
                        Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getDataSet(DataType.TYPE_STEP_COUNT_DELTA));
                        Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getBuckets().get(0));
                        Log.d("TAG_F", "onSuccess: 1 " + dataReadResponse.getBuckets().get(0).getDataSets().size());

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d("TAG_F", "onFailure: 1 " + e.getMessage());
                    }
                })
                .addOnCompleteListener(new OnCompleteListener<DataReadResponse>() {
                    @Override
                    public void onComplete(@NonNull Task<DataReadResponse> task) {
                        Log.d("TAG_F", "onComplete: 1 ");
                    }
                });

这篇关于如何从 Google Fit API 获取步骤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆