如何在活动开始一段时间后捕获方向值

时间:2014-03-24 20:28:07

标签: android timer android-sensors

我试图在活动开始后5秒读取方向和加速度计读数。我正在跟踪传感器读数,因为它们正在改变,但活动开始5秒钟,我想在某个位置捕获某些特殊读数。我希望所有其他进程在此期间正常工作。这是我的活动代码,我这样做了吗?

public class WorkoutBuddy extends Activity implements SensorEventListener {

    TextView t1, t2, t3; 
    Compass myCompass;
    SensorManager sensorManager;;
    private Sensor sensorAccelerometer;
    private Sensor sensorMagneticField;
    private float[] valuesAccelerometer;
    private float[] valuesMagneticField;
    private float[] startingPositionAccelerometer;
    private float[] startingPositionMagneticField;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exercise_buddy);
        t1 = (TextView)findViewById(R.id.textView1);
        t2 = (TextView)findViewById(R.id.textView2);
        t3 = (TextView)findViewById(R.id.textView3);
        myCompass = (Compass) findViewById(R.id.mycompass);

        sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        sensorAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorMagneticField = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

        valuesAccelerometer = new float[3];
        valuesMagneticField = new float[3];

        matrixR = new float[9];
        matrixI = new float[9];
        matrixValues = new float[3];
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        sensorManager.unregisterListener(this,sensorAccelerometer);
        sensorManager.unregisterListener(this,sensorMagneticField);
        super.onPause();
    }

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

    float azimuth,pitch,roll;
    private float[] matrixR;
    private float[] matrixI;
    private float[] matrixValues;
    boolean startingPosition = false;

    @Override
    public void onSensorChanged(SensorEvent event) {


        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            valuesAccelerometer = lowPass(event.values.clone(), valuesAccelerometer);
        } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            valuesMagneticField = lowPass(event.values.clone(), valuesMagneticField);
        }
        if (valuesAccelerometer != null && valuesMagneticField != null) {
            SensorManager.getRotationMatrix(matrixR, matrixI, valuesAccelerometer, valuesMagneticField);

            if(true){
                SensorManager.getOrientation(matrixR, matrixValues);

                double azimuth = Math.toDegrees(matrixValues[0]);
                double pitch = Math.toDegrees(matrixValues[1]);
                double roll = Math.toDegrees(matrixValues[2]);

                t1.setText("Azimuth: " + String.format("%.4f", azimuth));
                t2.setText("Pitch: " + String.format("%.4f", pitch));
                t3.setText("Roll: " + String.format("%.4f", roll));

                myCompass.update(matrixValues[0]);

                new java.util.Timer().schedule( 
                        new java.util.TimerTask() {
                            @Override
                            public void run() {
                                if(startingPosition == false){
                                    startingPositionAccelerometer= valuesAccelerometer;
                                    startingPositionMagneticField= valuesMagneticField;
                                    startingPosition = true;
                                }
                            }
                        }, 
                        5000 
                );



            }

        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        sensorManager.registerListener(this,sensorAccelerometer,SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(this,sensorMagneticField,SensorManager.SENSOR_DELAY_NORMAL);
        super.onResume();
    }

    //Low pass filter used to smooth the sensor readings
    protected float[] lowPass( float[] input, float[] output ) {
        float ALPHA = 0.25f;
        if ( output == null ) return input;     
        for ( int i=0; i<input.length; i++ ) {
            output[i] = output[i] + ALPHA * (input[i] - output[i]);
        }
        return output;
    }


}

2 个答案:

答案 0 :(得分:0)

您可以使用Handler执行延迟操作。

public void onResume() {
    super.onResume();
    new Handler().postDelayed(new Runnable() {

        public void run() {
                //Record readings here.
        }

    }, 5000);

答案 1 :(得分:0)

我更喜欢使用下面的ScheduledExecutorService ......

   ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2); // 2 - number of thread in pool

    // run every 5 seconds
    executorService.scheduleAtFixedRate(new Runnable() {
        public void run() {

            runOnUiThread(new Runnable() {
                public void run() {
                    // do your UI stuff here..
                }
            });
        }
    }, 0, 5, TimeUnit.SECONDS);