Google Fit与某些设备有关

时间:2017-07-26 13:45:19

标签: android google-fit google-fit-sdk

我创建了一个计步器,可以实时计算/显示步数,但它在某些设备上不起作用。到目前为止,它在Nexus 7平板电脑和三星Galaxy S7上运行得非常好,但在两种不同的Galaxy S4上却没有。

问题是屏幕关闭时不会在后台跟踪步骤。这也发生在Google Fit应用中。基本的想法是我有一个数据监听器,它检查步骤并增加计数。

这是步数逻辑。

public void buildFitnessClient() {
    // Create the Google API Client
    mClient = new GoogleApiClient.Builder(getActivity())
            .addApi(Fitness.RECORDING_API)
            .addApi(Fitness.HISTORY_API)
            .addApi(Fitness.SENSORS_API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
            .addConnectionCallbacks(
                    new GoogleApiClient.ConnectionCallbacks() {

                        @Override
                        public void onConnected(Bundle bundle) {
                            Log.i(TAG, "Connected!!!");
                            // Now you can make calls to the Fitness APIs.  What to do?
                            // Subscribe to some data sources!
                            subscribe();
                            readData();
                        }

                        @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.w(TAG, "Connection lost.  Cause: Network Lost.");
                            } else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
                                Log.w(TAG, "Connection lost.  Reason: Service Disconnected");
                            }
                        }
                    }
            )
            .enableAutoManage(getActivity(), 0, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    Log.w(TAG, "Google Play services connection failed. Cause: " +
                            result.toString());
                }
            })
            .build();

    //subscribe();

    //readData();
}

/**
 * Record step data by requesting a subscription to background step data.
 */
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(mClient, DataType.TYPE_STEP_COUNT_DELTA)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    if (status.isSuccess()) {
                        if (status.getStatusCode()
                                == FitnessStatusCodes.SUCCESS_ALREADY_SUBSCRIBED) {
                            Log.i(TAG, "Existing subscription for activity detected.");
                        } else {
                            Log.i(TAG, "Successfully subscribed!");
                        }
                    } else {
                        Log.w(TAG, "There was a problem subscribing.");
                    }
                }
            });
}

@Override
public void onResume() {
    super.onResume();

    String s = presenter.getStepCount();
    mInitialNumberOfSteps = Integer.parseInt(s);

    double cals = (_totalCalories - (mInitialNumberOfSteps * 0.044));
    int bSteps = (int) Math.round(cals);


    double eightyPercent = _totalCalories * 0.8;
    _eighty = (int) Math.round(eightyPercent);

    double sixtyPercent = _totalCalories * 0.6;
    _sixty = (int) Math.round(sixtyPercent);

    double fortyPercent = _totalCalories * 0.4;
    _forty = (int) Math.round(fortyPercent);

    double twentyPercent = _totalCalories * 0.2;
    _twenty = (int) Math.round(twentyPercent);

    if (bSteps <= eighty && bSteps > sixty) {
        _alreadyPlayed = false;
    } else if (bSteps <= sixty && bSteps > forty) {
        _alreadyPlayed = true;
    } else if (bSteps <= forty && bSteps > twenty) {
        _alreadyPlayed = false;
    } else if (bSteps <= twenty && bSteps > 0) {
        _alreadyPlayed = true;
    } else if (bSteps == 0 || bSteps < 0) {
        _alreadyPlayed = false;
    }



    //dispatcher.cancelAll();


    if (presenter.checkPauseBool()) {
        String calsRem = presenter.getCalsRemaining();

        _remCals = Integer.valueOf(calsRem);

        if (!calsRem.equals("0")) {

            _caloriesRemaining.setText(getResources().getString(R.string.crumble_calories_remaining, String.valueOf(calsRem)));
        }
    }

public int startPedometer() {
    listener = new OnDataPointListener() {
        @Override
        public void onDataPoint(DataPoint dataPoint) {
            for (Field field : dataPoint.getDataType().getFields()) {
                if (dataPoint.getDataType().equals(DataType.TYPE_STEP_COUNT_DELTA)) {
                    Value val = dataPoint.getValue(field);
                    Log.d("JUNE 12: ", "" + val);

                    presenter.setActiveFlag();

                    if (!presenter.checkPauseBool()) {
                        val.setInt(0);
                        presenter.putPauseBool();
                    }

                    mInitialNumberOfSteps += val.asInt();

                    //readData();

                    //_googleFit = new GoogleFit(getContext());


                    //int googleFitSteps = _googleFit.getSteps(_startTime);


                    if (getActivity() == null) {
                        return;
                    } else if (isAdded()) {
                        getActivity().runOnUiThread(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            //_dailyStepTotal.setText("Steps: " + mInitialNumberOfSteps);

                                                            double cals = (_totalCalories - (mInitialNumberOfSteps * 0.044));
                                                            _remCals = (int) Math.round(cals);

                                                            if (isAdded()) {
                                                                _caloriesRemaining.setText(getResources().getString(R.string.crumble_calories_remaining, String.valueOf(_remCals)));
                                                            }

                                                            double eightyPercent = _totalCalories * 0.8;
                                                            eighty = (int) Math.round(eightyPercent);

                                                            double sixtyPercent = _totalCalories * 0.6;
                                                            sixty = (int) Math.round(sixtyPercent);

                                                            double fortyPercent = _totalCalories * 0.4;
                                                            forty = (int) Math.round(fortyPercent);

                                                            double twentyPercent = _totalCalories * 0.2;
                                                            twenty = (int) Math.round(twentyPercent);

                                                            if ((_remCals <= eighty && _remCals > sixty) && !_alreadyPlayed) {
                                                                presenter.onBeginCrumbleClick();
                                                                presenter.switchCrumbleImage(two);
                                                                _alreadyPlayed = true;
                                                            } else if ((_remCals <= sixty && _remCals > forty) && _alreadyPlayed) {
                                                                presenter.onBeginCrumbleClick();
                                                                presenter.switchCrumbleImage(three);
                                                                _alreadyPlayed = false;
                                                            } else if ((_remCals <= forty && _remCals > twenty) && !_alreadyPlayed) {
                                                                presenter.onBeginCrumbleClick();
                                                                presenter.switchCrumbleImage(four);
                                                                _alreadyPlayed = true;
                                                            } else if ((_remCals <= twenty && _remCals > 0) && _alreadyPlayed) {
                                                                presenter.onBeginCrumbleClick();
                                                                presenter.switchCrumbleImage(five);
                                                                _alreadyPlayed = false;
                                                            } else if ((_remCals == 0 || _remCals < 0) && !_alreadyPlayed) {
                                                                presenter.clearActiveFoodFlag();
                                                                presenter.addToHistory(_totalCalories);
                                                                presenter.saveSteps("0");
                                                                presenter.updateQueue();
                                                                fireworksAnimation();
                                                                _alreadyPlayed = true;
                                                                getActivity().finish();
                                                            }
                                                        }
                                                    }
                        );
                    }

                }
            }
        }
    };
    SensorRequest req = new SensorRequest.Builder().setDataType(DataType.TYPE_STEP_COUNT_DELTA).setSamplingRate(1, TimeUnit.SECONDS).build();
    result = Fitness.SensorsApi.add(mClient, req, listener);

    //Log.d("Steps: ", "" + result);

    return mInitialNumberOfSteps;
}

0 个答案:

没有答案