在Android中使用Google fit Recording_API远距离行走

时间:2017-06-28 19:41:58

标签: google-fit google-fit-sdk

我正在制作一个健身应用,我需要在其中获取用户移动的步数和距离。为此我通过订阅步数和距离来使用Google fit RECORDING_API,我成功地获得了步数,但无法获得距离。这是我的代码

Fitness.RecordingApi.subscribe(googleApiClient, DataType.TYPE_DISTANCE_DELTA)
                        .setResultCallback(new ResultCallback<Status>() {
                            @Override
                            public void onResult(Status status) {
                                if (status.isSuccess()) {
                                    if (status.getStatusCode() == FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
                                        Log.e( "RecordingAPI", "Already subscribed to get Distance Recording API");
                                    } else {
                                        Log.e("RecordingAPI", "Subscribed to get distance using Recording API");
                                    }
                                }
                                new FetchDistanceAsync().execute();
                            }
                        });




private class FetchDistanceAsync extends AsyncTask<Object, Object, Float>{
    @Override
    protected Float doInBackground(Object... objects) {
        float distance = 0;
        PendingResult<DailyTotalResult> result = Fitness.HistoryApi.readDailyTotal(googleApiClient, DataType.TYPE_DISTANCE_DELTA);
        DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
        if (totalResult.getStatus().isSuccess()) {
            DataSet totalSet = totalResult.getTotal();
            if (totalSet != null) {
                distance = totalSet.isEmpty() ? 0 : totalSet.getDataPoints().get(0).getValue(Field.FIELD_DISTANCE).asFloat();
            }
        } else {
            Log.w(TAG, "There was a problem getting the calories.");
        }
        return distance;
    }

    @Override
    protected void onPostExecute(Float aLong) {
        super.onPostExecute(aLong);
        float distance = aLong/1000;
        double dis = Math.round(distance * 100.0) / 100.0;
        tvKilometer.setText(String.valueOf(dis));
    }
}

距离回到了我0.实际上我移动了184步但仍然距离返回值0.你能帮我解决这个问题。

0 个答案:

没有答案