如何使用Android版Google Fit API查找步骤?

问题描述 投票:2回答:2

我已经做了几天的研究。我所需要的只是我的应用程序中的Simple TextView区域,以显示当今的步骤。

我设法使身份验证与下面的代码一起使用。弹出请求许可,并认为我选择了正确的一个。

但是我不知道如何简单地获取步数信息。我希望这只是几行代码。任何帮助,将不胜感激。谢谢

编辑1:我只需要获取步数。我可以弄清楚以后如何显示它。我也有吐司只是为了帮助我弄清楚发生了什么。

private void buildFitnessClient() {
    if (mClient == null) {
        mClient = new GoogleApiClient.Builder(this)
                .addApi(Fitness.SENSORS_API)
                .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ))
                .addConnectionCallbacks(
                        new GoogleApiClient.ConnectionCallbacks() {
                            @Override
                            public void onConnected(Bundle bundle) {
                                Log.i(TAG, "Connected!!!");
                                // Now you can make calls to the Fitness APIs.
                                Toast.makeText(getBaseContext(), "Connected!", Toast.LENGTH_LONG).show();


                            }

                            @Override
                            public void onConnectionSuspended(int i) {
                                // If your connection to the sensor gets lost at some point,
                                // you'll be able to determine the reason and react to it here.
                                if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
                                    Log.i(TAG, "Connection lost.  Cause: Network Lost.");
                                    Toast.makeText(getBaseContext(), "Connection lost.  Cause: Network Lost.", Toast.LENGTH_LONG).show();

                                } else if (i
                                        == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
                                    Log.i(TAG,
                                            "Connection lost.  Reason: Service Disconnected");
                                }
                            }
                        }
                )
                .enableAutoManage(this, 0, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(ConnectionResult result) {
                        Log.i(TAG, "Google Play services connection failed. Cause: " +
                                result.toString());
                        Toast.makeText(getBaseContext(), "Google Play services connection failed. Cause: " +
                                result.toString(), Toast.LENGTH_LONG).show();

                    }
                })
                .build();
    }
}
java android api google-fit
2个回答
1
投票

从Google上查看此official documentation,以了解如何从Fit中读取数据:

// Setting a start and end date using a range of 1 week before this moment.
Calendar cal = Calendar.getInstance();
Date now = new Date();
cal.setTime(now);
long endTime = cal.getTimeInMillis();
cal.add(Calendar.WEEK_OF_YEAR, -1);
long startTime = cal.getTimeInMillis();

java.text.DateFormat dateFormat = getDateInstance();
Log.i(TAG, "Range Start: " + dateFormat.format(startTime));
Log.i(TAG, "Range End: " + dateFormat.format(endTime));

DataReadRequest readRequest = new DataReadRequest.Builder()
        // The data request can specify multiple data types to return, effectively
        // combining multiple data queries into one call.
        // In this example, it's very unlikely that the request is for several hundred
        // datapoints each consisting of a few steps and a timestamp.  The more likely
        // scenario is wanting to see how many steps were walked per day, for 7 days.
        .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
        // Analogous to a "Group By" in SQL, defines how data should be aggregated.
        // bucketByTime allows for a time span, whereas bucketBySession would allow
        // bucketing by "sessions", which would need to be defined in code.
        .bucketByTime(1, TimeUnit.DAYS)
        .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
        .build();

[History API Sample app on GitHub:

在GitHub here上检查示例项目。

直接链接到MainActivity.java(在上面的示例项目中),其中包含所需的代码:Link


0
投票

GoogleApiClient已根据新更新弃用。以及HistoryApi也已弃用。因此,首先必须使用GoogleSignInAccount而不是GoogleApiClient,还必须使用Google Fit的HistoryClient而不是HistoryApi。谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.