其他分享
首页 > 其他分享> > 如何使用Google Fit的API获取用户当前的速度?

如何使用Google Fit的API获取用户当前的速度?

作者:互联网

目前使用Google Fit API并且在使用Sensors API时遇到一些问题.我正在尝试获取用户当前的应用程序锻炼速度,但文档有点令人困惑.

在此代码段中是Google信息页面中的示例:

Fitness.SensorsApi.add(
            mClient,
            new SensorRequest.Builder()
                    // Optional but recommended for custom data sets.
                    .setDataType(DataType.TYPE_SPEED)// Can't be omitted.
                    .setSamplingRate(1, TimeUnit.SECONDS).setAccuracyMode(SensorRequest.ACCURACY_MODE_HIGH)
                    .build(), mListener3)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    if (status.isSuccess()) {
                        Log.i(TAG, "Listener registered!");
                    } else {
                        Log.i(TAG, "Listener not registered.");
                    }
                }
            });

//添加监听器

mListener3 = new OnDataPointListener() {
        @Override
        public void onDataPoint(DataPoint dataPoint) {


           final float speed = dataPoint.getValue(Field.FIELD_SPEED).asFloat();

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    Log.i(TAG, "In Speed" + speed );
                    speedTxtView.setText("" + speed );

                }
            });


        }

目前,我正在获取所有其他数据类型值,如距离,心率,步数和当前活动,但无法获得用户当前的速度.
我做得对吗?

解决方法:

您可以尝试使用Google Fit Github存储库中的basichistorysessions sample.

示例代码:

  // Build a session read request
        SessionReadRequest readRequest = new SessionReadRequest.Builder()
                .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)
                .read(DataType.TYPE_SPEED)
                .setSessionName(SAMPLE_SESSION_NAME)
                .build();


// Invoke the Sessions API to fetch the session with the query and wait for the result
                    // of the read request.
                    SessionReadResult sessionReadResult =
                            Fitness.SessionsApi.readSession(mClient, readRequest)
                                    .await(1, TimeUnit.MINUTES);

                    // Get a list of the sessions that match the criteria to check the result.
                    Log.i(TAG, "Session read was successful. Number of returned sessions is: "
                            + sessionReadResult.getSessions().size());
                    for (Session session : sessionReadResult.getSessions()) {
                        // Process the session
                        dumpSession(session);

                        // Process the data sets for this session
                        List<DataSet> dataSets = sessionReadResult.getDataSet(session);
                        for (DataSet dataSet : dataSets) {
                            dumpDataSet(dataSet);
                        }
                    }

有关更多信息,请参阅此reading fitness data using sessions部分.

标签:java,android,google-maps,google-fit
来源: https://codeday.me/bug/20190628/1319273.html