0

我想测量一次大震动(例如:测量自行车或汽车发生事故时的情况。我想测量手机震动)。当我的手机摇晃时,我有播放音频的代码。但是当我将 SHAKE_THRESHOLD 值更改为高水平时,它不起作用。是否可以测量大震动。

我的代码:

public class ShakeActivity extends Activity implements SensorListener{

     // For shake motion detection.
    private MediaPlayer mPlayer1;
    private SensorManager sensorMgr;
    private long lastUpdate = -1;
    private float x, y, z;
    private float last_x, last_y, last_z;
    private static final int SHAKE_THRESHOLD = 8000;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         // other initializations
        // start motion detection
        sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
        boolean accelSupported = sensorMgr.registerListener(this,
            SensorManager.SENSOR_ACCELEROMETER,
            SensorManager.SENSOR_DELAY_GAME);

        if (!accelSupported) {
            // on accelerometer on this device
            sensorMgr.unregisterListener(this,
                    SensorManager.SENSOR_ACCELEROMETER);
        }
    }

    protected void onPause() {
        if (sensorMgr != null) {
            sensorMgr.unregisterListener(this,
                    SensorManager.SENSOR_ACCELEROMETER);
            sensorMgr = null;
            }
        super.onPause();
        }


    @Override
    public void onAccuracyChanged(int arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(int sensor, float[] values) {
        // TODO Auto-generated method stub

        if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
            long curTime = System.currentTimeMillis();
            // only allow one update every 100ms.
            if ((curTime - lastUpdate)> 100) {
            long diffTime = (curTime - lastUpdate);
            lastUpdate = curTime;

            x = values[SensorManager.DATA_X];
            y = values[SensorManager.DATA_Y];
            z = values[SensorManager.DATA_Z];

            float speed = Math.abs(x+y+z - last_x - last_y - last_z)
                                  / diffTime * 10000;
            if (speed > SHAKE_THRESHOLD) {
                // yes, this is a shake action! Do something about it!

                MediaPlayer mPlayer1 = MediaPlayer.create(this, R.raw.hello);

                try {
                    mPlayer1.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                mPlayer1.start();




            }
            last_x = x;
            last_y = y;
            last_z = z;
            }
        }
        }

    } 
4

1 回答 1

1

转到API Demos application -> OS -> Sensors 那里有一个图表。摇动您的设备,看看哪个传感器显示您的设备变化更好并满足您的需求。

于 2012-09-19T05:24:16.067 回答