从谷歌获取适合步数 [英] Getting step count from google fit

查看:235
本文介绍了从谷歌获取适合步数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用谷歌适合我的应用程序,并试图获得步数。注册和authantication事情都OK。您的谷歌帐户的时候,您选择它,然后在出现的谷歌权限对话框,选择yes后。这一点后,以下方法被调用,它使用听者寄存器从步骤计数器的数据资源的选择等的实时更新。然而,步数是有点好笑。有时,它是这样上下。也许我没有使用任何录音两不误。有什么建议吗?

 私人无效invokeFitnessAPIs()​​{    //创建一个侦听器对象被调用时,新数据可用
    OnDataPointListener监听器=新OnDataPointListener(){
        @覆盖
        公共无效onDataPoint(数据点数据点){            对于(场场:dataPoint.getDataType()getFields()){
                值val = dataPoint.getValue(场);
                updateTextViewWithStepCounter(val.asInt());
            }
        }
    };    //指定哪些数据源返回
    DataSourcesRequest REQ =新DataSourcesRequest.Builder()
            .setDataSourceTypes(DataSource.TYPE_DERIVED)
            .setDataTypes(DataType.TYPE_STEP_COUNT_DELTA)
            。建立();    //调用与传感器API:
    // - 谷歌的API客户端对象
    // - 数据源请求对象
    PendingResult< D​​ataSourcesResult> pendingResult =
            Fitness.SensorsApi.findDataSources(mClient,REQ);    //建立一个传感器注册请求对象
    SensorRequest sensorRequest =新SensorRequest.Builder()
            .setDataType(DataType.TYPE_STEP_COUNT_CUMULATIVE)
            .setSamplingRate(1,TimeUnit.SECONDS)
            。建立();    //调用与传感器API:
    // - 谷歌的API客户端对象
    // - 传感器注册请求对象
    // - 侦听器对象
    PendingResult<状态> regResult =
            Fitness.SensorsApi.add(mClient,
                    新SensorRequest.Builder()
                            .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
                            .setSamplingRate(1,TimeUnit.SECONDS)
                            。建立(),
                    监听器);
    // 4.检查结果异步
    regResult.setResultCallback(新ResultCallback<状态>()
    {
        @覆盖
        公共无效onResult(状态状态){
            如果(status.isSuccess()){
                Log.d(TAG,听众注册);
                //注册监听器
            }其他{
                Log.d(TAG,监听器未注册);
                //监听未注册
            }
        }
    });
  }  //更新文本查看器有步骤的柜台..
  私人无效updateTextViewWithStepCounter(最终诠释numberOfSteps){
    runOnUiThread(新的Runnable(){
        @覆盖
        公共无效的run(){
            Toast.makeText(getBaseContext(),关于数据点!,
 Toast.LENGTH_SHORT);            如果(mFirstCount&放大器;&放大器;!(numberOfSteps = 0)){
                mInitialNumberOfSteps = numberOfSteps;
                mFirstCount = FALSE;
            }
            如果(TextView的!= NULL){
                textView.setText(将String.valueOf(numberOfSteps));
            }
        }
    });
 }
 @覆盖
 保护无效的onSaveInstanceState(捆绑outState){
    super.onSaveInstanceState(outState);
    outState.putBoolean(AUTH_PENDING,authInProgress);
 } }


解决方案

您正在使用的数据源和传感器的要求不同的数据类型

I am using google fit for my application and trying to get the step count. The registration and authantication things are ok. Your google account comes up you select it and after that the google permission dialog appears and select yes. After that point the following method is called which uses the listener to register the live updates from the step counter,the DataResource selection and so on. However the step count is a bit funny. Sometimes it goes like up and down. Maybe I don't use any of the recording things correct. Any advice please?

    private void invokeFitnessAPIs() {

    // Create a listener object to be called when new data is available
    OnDataPointListener listener = new OnDataPointListener() {
        @Override
        public void onDataPoint(DataPoint dataPoint) {

            for (Field field : dataPoint.getDataType().getFields()) {
                Value val = dataPoint.getValue(field);
                updateTextViewWithStepCounter(val.asInt());
            }
        }
    };

    //Specify what data sources to return
    DataSourcesRequest req = new DataSourcesRequest.Builder()
            .setDataSourceTypes(DataSource.TYPE_DERIVED)
            .setDataTypes(DataType.TYPE_STEP_COUNT_DELTA)
            .build();

    //  Invoke the Sensors API with:
    // - The Google API client object
    // - The data sources request object
    PendingResult<DataSourcesResult> pendingResult =
            Fitness.SensorsApi.findDataSources(mClient, req);

    //  Build a sensor registration request object
    SensorRequest sensorRequest = new SensorRequest.Builder()
            .setDataType(DataType.TYPE_STEP_COUNT_CUMULATIVE)
            .setSamplingRate(1, TimeUnit.SECONDS)
            .build();

    //  Invoke the Sensors API with:
    // - The Google API client object
    // - The sensor registration request object
    // - The listener object
    PendingResult<Status> regResult =
            Fitness.SensorsApi.add(mClient,
                    new SensorRequest.Builder()
                            .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
                            .setSamplingRate(1,TimeUnit.SECONDS)
                            .build(),
                    listener);


    // 4. Check the result asynchronously
    regResult.setResultCallback(new ResultCallback<Status>()
    {
        @Override
        public void onResult(Status status) {
            if (status.isSuccess()) {
                Log.d(TAG, "listener registered");
                // listener registered
            } else {
                Log.d(TAG, "listener not registered");
                // listener not registered
            }
        }
    });
  }

  // Update the Text Viewer with Counter of Steps..
  private void updateTextViewWithStepCounter(final int numberOfSteps) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getBaseContext(), "On Datapoint!", 
 Toast.LENGTH_SHORT);

            if(mFirstCount && (numberOfSteps != 0)) {
                mInitialNumberOfSteps = numberOfSteps;
                mFirstCount = false;
            }
            if(textView != null){
                textView.setText(String.valueOf(numberOfSteps));
            }
        }
    });
 }
 @Override
 protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean(AUTH_PENDING, authInProgress);
 }

 }

解决方案

You are using different data types for data source and sensor request

这篇关于从谷歌获取适合步数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆