摇不动超过一定的价值

时间:2012-09-19 05:04:18

标签: android accelerometer android-sensors shake

我想测量一下大摇晃(例如:当自行车或汽车发生意外时测量。我想测量手机摇晃)。当我的手机动摇时,我有播放音频的代码。但是当我将SHAKE_THRESHOLD值更改为高级别时,它无法正常工作。 是否有可能测量大震动。

mycode的:

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

    } 

1 个答案:

答案 0 :(得分:1)

转到 API演示应用程序 - > OS - >传感器那里有一个图表。摇动您的设备,看看哪个传感器可以更好地显示您的设备,并满足您的需求。