Shake like WeChat

Keywords: Android

Shake function

1. Get Sensor Management Classes

    SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

2. Inherit the SensorEventListener interface and override sensor listening methods

    @Override
    public void onSensorChanged(SensorEvent event) {
        /**
         * 1,Get the sensor type (shake if accelerometer)
         * 2,Judging its acceleration
         */
        int type = event.sensor.getType();
        if (type != Sensor.TYPE_ACCELEROMETER) {
            return;
        }

        long currentTime = System.currentTimeMillis();
        long diffTime = currentTime - mLastUpdateTime;
        //UPDATE_INTERVAL=100 detection interval
        if (diffTime < UPDATE_INTERVAL) {
            return;
        }
        mLastUpdateTime = currentTime;
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];
        //When mLastX, mLastY, mLastZ was last detected, the component of acceleration in the x, y, z direction was used to compare with the current acceleration.
        float deltaX = x - mLastX;
        float deltaY = y - mLastY;
        float deltaZ = z - mLastZ;
        mLastX = x;
        mLastY = y;
        mLastZ = z;
        float delta = (float) (Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ) / diffTime * 10000);
        // When the difference in acceleration is greater than the specified threshold, this is considered a shake
        if (delta > 500) {
            Message message = Message.obtain();
            message.what = 1;
            mHandler.sendMessage(message);
        }
    }

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

    }

3. Register listeners

    if (sensorManager != null) {
        Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        if (sensor != null) {
            sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
        }
    }
//  sensorManager.registerListener(listener,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);

4. Cancel the listener

    sensorManager.unregisterListener(listener);

Add a shake effect

1. Initialize sound, add sound file

    soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);//Use below 5.0
    soundId = soundPool.load(context, R.raw.shake_sound, 1);

2. Play sound after shaking

    soundPool.play(soundId,
            1f, //Left Channel [0~1]
            1f, //Right Channel [0~1]
            1,//Play priority [0 means lowest priority]
            0,//Number of cycles [0 for once, -1 for infinite times]
            1);//Play speed 0 is normal speed [0~2]

Add a shake

1. Initialization Class

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

2. Add vibration after shaking

    vibrator.vibrate(300);//300milliseconds

Note: Add permissions

<!--Shake the permissions you need-->
    <uses-permission android:name="android.hardware.sensor.accelerometer" />
<!--Permissions required for vibration-->
    <uses-permission android:name="android.permission.VIBRATE" />




Posted by thomasadam83 on Sat, 18 Jul 2020 09:06:41 -0700