0

我想计算android手机用户的步数(基本上我想实现一个基本的计步器)。我使用的逻辑是,如果 z 方向上的加速度变化大于某个阈值,则必须将其计为一步。

我的应用程序使用以下代码使用加速度计来计算所采取的步数

package com.bhaskar.workout_helper_app;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

   public class CountActivity extends Activity implements SensorEventListener {

private float mLastZ,mLastY,mLastX;
private int stepcount;
private TextView tv;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private Boolean mInitialized;

// constants
private final float LEG_THRSHOLD_AMPLITUDE = (float) 5.0;
private static final float NOISE = (float) 2.0;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.countactivity);

    // initialise variables
    stepcount = 0;
    tv = (TextView) findViewById(R.id.textView3);
    mInitialized=false;
    // initialise sensor manager and check whether accelerometer is present
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager
            .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    if (mAccelerometer == null) {
        Log.d("HARDWARE", "ACCELEROMETER NOT PRESENT!!!");
        startActivity(new Intent(CountActivity.this, workoutdata.class));
    } else {
        if (mAccelerometer.getMinDelay() != 0) {
            Log.d("ACCELEROMETER",
                    "reports data only when the value of accelerometer is changed!!!");
            //tv.setText("at regular interval "+SensorManager.SENSOR_DELAY_NORMAL);
        }
        mSensorManager.registerListener(this, mAccelerometer,
                SensorManager.SENSOR_DELAY_NORMAL);
    }
    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            tv.setText(stepcount);
            // TODO Auto-generated method stub
            startActivity(new Intent(CountActivity.this, MainActivity.class));
        }
    });
}

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

}

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
        return;
    }
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];


    if(Math.abs(z) > LEG_THRSHOLD_AMPLITUDE)
        stepcount++;
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    if (mAccelerometer != null)
        mSensorManager.registerListener(this, mAccelerometer,
                SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    if (mAccelerometer != null)
        mSensorManager.unregisterListener(this);
}
}

但是每当我在手机上测试它时它就会崩溃,请帮助我该怎么办?

4

1 回答 1

0

用这个

  tv.setText(""+stepcount); 

代替

     tv.setText(stepcount);
于 2013-10-17T04:58:45.017 回答