我需要为正在执行的项目计步器,因此我遵循了本教程:http://blog.bawa.com/2013/11/create-your-own-simple-pedometer.html

不幸的是,即使遵循其中的每一行代码,它也不起作用。我的意思是,没有步骤被计算或检测到。不幸的是,我无法与本教程的创建者联系,因此需要尽快。任何人都不知道可能是什么问题?我缺少重要的东西吗?

这是代码:

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);
     }

}

最佳答案

Nexus 7没有内置的计步器传感器,这是您以前使用的代码所必需的。

请注意,博客文章的作者使用的是Nexus 5,它确实具有这种传感器。

关于java - 简单的Android计步器无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23520814/

10-10 08:22