首页 > 解决方案 > googleFit 获取每小时计步器更新回调?

问题描述

我将 googleFit 集成到 android 应用程序中。我正在使用历史 API 来按需获取更新,并订阅更新。这是我添加的代码片段。我正在使用初始化 google fit 并获取计步器。我需要从计步器挂起的单位时间回调以发送到服务器。

 mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Fitness.SESSIONS_API)
            .addApi(Fitness.HISTORY_API)
            .addApi(Fitness.RECORDING_API)
            .addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE))
            .addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
            .addConnectionCallbacks(this)
            .enableAutoManage(this, 0, this)
            .build();

public void subscribe() {
        // To create a subscription, invoke the Recording API. As soon as the subscription is
        // active, fitness data will start recording.
        Fitness.RecordingApi.subscribe(mGoogleApiClient, DataType.TYPE_STEP_COUNT_CUMULATIVE)
                .setResultCallback(status -> {
                    if (status.isSuccess()) {
                        if (status.getStatusCode()
                                == FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
                            Log.e(TAG, "Existing subscription for activity detected.");
                        } else {
                            Log.e(TAG, "Successfully subscribed!");
                        }
                    } else {
                        Log.w(TAG, "There was a problem subscribing.");
                    }
                });
    }

public void unSubscribe() {
    // To create a subscription, invoke the Recording API. As soon as the subscription is
    // active, fitness data will start recording.
    Fitness.RecordingApi.unsubscribe(mGoogleApiClient, DataType.TYPE_STEP_COUNT_CUMULATIVE)
            .setResultCallback(status -> {
                if (status.isSuccess()) {
                    if (status.getStatusCode()
                            == FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
                        Log.e(TAG, "Existing subscription for activity detected.");
                    } else {
                        Log.e(TAG, "Successfully subscribed!");
                    }
                } else {
                    Log.w(TAG, "There was a problem subscribing.");
                }
            });
}




long total = 0;
    PendingResult<DailyTotalResult> result = Fitness.HistoryApi.readDailyTotal(mGoogleApiClient, DataType.TYPE_STEP_COUNT_DELTA);
    DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
    if (totalResult.getStatus().isSuccess()) {
        DataSet totalSet = totalResult.getTotal();
        total = totalSet.isEmpty()
                ? 0
                : totalSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
    } else {
        Log.e(TAG, "There was a problem getting the step count.");
    }

    Log.e(TAG, "TOTAL STEPS : " + total);

    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
        Log.e(TAG, "onConnection DisConnected");
    }

标签: androidgoogle-fitgoogle-fit-sdk

解决方案


推荐阅读