需要该应用程序检测来回摇动,当前仅检测一次摇动

时间:2019-06-30 19:29:33

标签: java android accelerometer

该代码检测到摇动/运动手势。我只需要它来检测来回抖动,在Moto智能手机中也称为“斩波”功能。 onSensorChanged方法需要一些工作。

尝试在shake_count方法上使用onSensorChanged变量,但是我想我错过了一些关键部分。 另外,如果加速度计传感器每次检测到所需的动作都暂停2秒,将会很有帮助。

public class MainActivity extends AppCompatActivity {

    private Button button;
    private boolean isFlashOn = false;
    private Camera.Parameters params;
    float shakeCount = 0;


    Camera camera;
    private SensorManager sm;
    private float acelValue;    // current acceleration value and gravity
    private float acelLast;     // last acceleration value and gravity
    private float shake;        // acceleration value differ from gravity


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        sm.registerListener(
            sensorListener,
            sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_NORMAL
        );

        acelValue = SensorManager.GRAVITY_EARTH;
        acelLast = SensorManager.GRAVITY_EARTH;
        shake = 0.00f;

        button = (Button) findViewById(R.id.onSwitch);
    }


    boolean click = false;

    public void onSwitchFunction(View view) {

        if (click == false) {
            button.setText("TURN OFF");
            Toast toast = Toast.makeText(getApplicationContext(), "Shake Features On", Toast.LENGTH_LONG);
                    toast.show();

            click = true;
        } else if (click == true) {
            button.setText("TURN ON");

            Toast toast = Toast.makeText(getApplicationContext(), "Shake Features Off", Toast.LENGTH_LONG);
                    toast.show();

            turnOffFlash();
            click = false;
        }
    }


    private final SensorEventListener sensorListener = new SensorEventListener() {

                @Override
                public void onSensorChanged(SensorEvent sensorEvent) {
                    float x = sensorEvent.values[0];
                    float y = sensorEvent.values[1];
                    float z = sensorEvent.values[2];

                    acelLast = acelValue;
                    acelValue = (float) Math.sqrt((double) (x * x + y * y + z * z));
                    float delta = acelValue - acelLast;
                    shake = shake + delta;

                    if (click) {

                        if (shake > 24) {
                            shakeCount++;
                            if (shakeCount > 1) {
                                shakeCount = 0;

                                Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

                                if (isFlashOn) {
                                    turnOffFlash();
                                } else if (!isFlashOn) {
                                    turnOnFlash();
                                }

                                vibrator.vibrate(300);
                            }

                        }

                    }
                }


                @Override
                public void onAccuracyChanged(Sensor sensor, int accuracy) {

                }
            };

    @TargetApi(Build.VERSION_CODES.M)
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void turnOffFlash() {
        CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

        try {
            String cameraId = cameraManager.getCameraIdList()[0];
            cameraManager.setTorchMode(cameraId, false);
        } catch (CameraAccessException e) {
        }

        isFlashOn = false;
    }

    @TargetApi(Build.VERSION_CODES.M)
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void turnOnFlash() {
        CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

        try {
            String cameraId = cameraManager.getCameraIdList()[0];
            cameraManager.setTorchMode(cameraId, true);
        } catch (CameraAccessException e) {
        }

        isFlashOn = true;
    }
}

我希望代码能够检测到一系列震动事件,例如。仅检测设备是否摇动3次。理想的输出将检测来回的震动。

1 个答案:

答案 0 :(得分:0)

检查此link,如果阈值超出限制并且 Z 值超过您提供的值,则应监视 Z 的值它将吐司消息。

public class ShakeActivity extends Activity implements SensorListener {
    // For shake motion detection.
    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 = 800;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    // 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();
    }

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

    public void onSensorChanged(int sensor, float[] values) {
    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];

        if(Round(x,4)>10.0000){
            Log.d("sensor", "X Right axis: " + x);
            Toast.makeText(this, "Right shake detected", Toast.LENGTH_SHORT).show();
        }
        else if(Round(x,4)<-10.0000){
            Log.d("sensor", "X Left axis: " + x);
            Toast.makeText(this, "Left shake detected", Toast.LENGTH_SHORT).show();
        }else  if(Round(z,4)>10.0000){
            Log.d("sensor", "Z Up axis: " + z);
            Toast.makeText(this, "Up shake detected", Toast.LENGTH_SHORT).show();
        }
        else if(Round(z,4)<-10.0000){
            Log.d("sensor", "Z Down axis: " + z);
            Toast.makeText(this, "Down shake detected", Toast.LENGTH_SHORT).show();

        float speed = Math.abs(x+y+z - last_x - last_y - last_z) / diffTime * 10000;

        // Log.d("sensor", "diff: " + diffTime + " - speed: " + speed);
        if (speed > SHAKE_THRESHOLD) {
            //Log.d("sensor", "shake detected w/ speed: " + speed);
            //Toast.makeText(this, "shake detected w/ speed: " + speed, Toast.LENGTH_SHORT).show();
        }
        last_x = x;
        last_y = y;
        last_z = z;
        }
    }
    }

    public static float Round(float Rval, int Rpl) {
    float p = (float)Math.pow(10,Rpl);
    Rval = Rval * p;
    float tmp = Math.round(Rval);
    return (float)tmp/p;
    }
}