1

I'm working with android sensors, specifically TYPE_STEP_COUNTER. The value returned by TYPE_STEP_COUNTER is constantly increasing and there is not way of resetting it (as far as I know). What I need to do is get the number of steps taken from the point the user presses a button.

I would do:

int steps = overallTotal - totalWhenButtonPressed

How can I get the totalWhenButtonPressed value?

I can't do totalWhenButtonPress = event.values[0] because the value keeps changing.

tranquilswan
  • 13
  • 1
  • 7
  • can you explain why you can't do `totalWhenButtonPress = event.values[0]` because that's exactly what I would do. Primitive values are copied, your `total...` does not change when the other one changes, in case that's what you're thinking. – zapl Nov 29 '15 at 04:50
  • @zapl I can't do totalWhenButtonPress = event.values[0] because event.values[0] keeps continuously updating. So if I reference totalWhenButtonPress later the value will be the new value at event.values[0] – tranquilswan Nov 29 '15 at 05:53
  • 1
    Exactly that's not the case. The moment that line executes, it copies the current value into `total..`. There is no connection between the two because `int` isn't a reference. And even if it was an object, you'd copy the reference to the current content of `values[0]`. You don't share the reference with `value[0]`. Everyone gets it's own reference to whatever it is. http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value explains – zapl Nov 29 '15 at 06:40
  • No dice, my code: 'Sensor stepCtr = sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER); sm.registerListener(mSensorCounter, stepCtr, SensorManager.SENSOR_DELAY_NORMAL);' 'private SensorEventListener mSensorCounter = new SensorEventListener() { public void onSensorChanged(SensorEvent event) { TextView txtStepVals = ... int ogSteps = Math.round(event.values[0]); double currSteps = event.values[0]; txtStepVals.setText("CurrSteps = " + currSteps + "\nOgSteps = " + ogSteps + "\nSteps: " + (currSteps - ogSteps)); } };' – tranquilswan Nov 29 '15 at 19:37
  • I can't read that :) Please [edit] your question and format it nicely. Ps `int ogSteps` is that a local variable in a callback that is overwritten every time this method is called? https://www.cs.umd.edu/~clin/MoreJava/Objects/local.html ("Local Variables Have No Memory") – zapl Nov 29 '15 at 19:43

2 Answers2

0

It should work roughly like this

private int mLatestValue;
private int mButtonPressedValue;

void onCreate(..) {
    Sensor stepCtr = sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
    sm.registerListener(mSensorCounter, stepCtr, SensorManager.SENSOR_DELAY_NORMAL);
    Button button = button.setOnClickListener(mButtonClick);
}

private OnClickListener mButtonClick = new OnClickListerner() {
    public void onClick(View view) {
        mButtonPressedValue = mLatestValue;
    }
}
private SensorEventListener mSensorCounter = new SensorEventListener() {
    public void onSensorChanged(SensorEvent event) {
        mLatestValue = Math.round(event.values[0]);
        txtStepVals.setText("CurrSteps = " + mLatestValue + "\nSteps: " + (mLatestValue - mButtonPressedValue));
    }
};

You need to store the current sensor outside of the SensorEventListener so you can store that value once the button is pressed. And then simply compare to the value you stored when the button was pressed.

zapl
  • 63,179
  • 10
  • 123
  • 154
0

I was able to solve the problem by using the TYPE_STEP_DETECTOR. Instead of trying to get the number at a point in time, I just updated the number as the action occurred. The following code is from the onSensorChanged method of the SensorEventListener

currSteps = currSteps + Math.round(event.values[0]);
        TextView txtSessionStepVals = (TextView) findViewById(R.id.txtSessionStepValues);
        txtSessionStepVals.setText("Steps this Session: " + currSteps);
tranquilswan
  • 13
  • 1
  • 7