문제

I need a pedometer for a project I am doing, so I followed this tutorial: http://blog.bawa.com/2013/11/create-your-own-simple-pedometer.html

Unfortunately even following every single line of code from it, it does not work. By that I mean, no steps are ever counted or detected. Unfortunately I can't get in touch with the creator of the tutorial and I need it ASAP. Anyone have any idea what might be the issue? Am I missing something important?

Here is the code:

private TextView textView;

private SensorManager mSensorManager;
private Sensor mStepCounterSensor;
private Sensor mStepDetectorSensor;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView)findViewById(R.id.textView);

    mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    mStepCounterSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
    mStepDetectorSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);

}

protected void onResume() {

     super.onResume();

     mSensorManager.registerListener(this, mStepCounterSensor, SensorManager.SENSOR_DELAY_FASTEST);      
     mSensorManager.registerListener(this, mStepDetectorSensor, SensorManager.SENSOR_DELAY_FASTEST);

 }

 protected void onStop() {
     super.onStop();
     mSensorManager.unregisterListener(this, mStepCounterSensor);
     mSensorManager.unregisterListener(this, mStepDetectorSensor);
 }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
     Sensor sensor = event.sensor;
     float[] values = event.values;
     int value = -1;

     if (values.length > 0) {
        value = (int) values[0];
     }

      if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
          textView.setText(" Step Counter Detected : " + value);
     } else if (sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
         // For test only. Only allowed value is 1.0 i.e. for step taken
         textView.setText(" Step Detector Detected : " + value);
     }

}
도움이 되었습니까?

해결책

The Nexus 7 does not have the built-in pedometer sensor that is needed for the code you used to work.

Notice the author of the blog post uses a Nexus 5, which does have such a sensor.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top