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

查看:498
本文介绍了如何从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天全站免登陆