How to use Shake device sensors In Android

In this android programming source code example, we are going to use Shake device sensors In Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to use Shake device sensors In Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DeviceFeatures.DeviceFeaturesActivity16">

    <TextView
        android:id="@+id/shakeDevice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Shake Device to Change Text"
        android:gravity="center"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

import com.bluapp.androidview2.R;

public class DeviceFeaturesActivity16 extends AppCompatActivity {
    private TextView shakeDevice;
    private SensorManager sensorManager;
    private ShakeEventListener shakeEventListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_features16);
        shakeDevice = (TextView) findViewById(R.id.shakeDevice);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        shakeEventListener = new ShakeEventListener();
        shakeEventListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {
            public void onShake() {
                shakeDevice.setText("Device Was Shake");
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(shakeEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    protected void onPause() {
        sensorManager.unregisterListener(shakeEventListener);
        super.onPause();
    }

    public static class ShakeEventListener implements SensorEventListener {
        private static final int MIN_FORCE = 10;
        private static final int MIN_DIRECTION_CHANGE = 3;
        private static final int MAX_PAUSE_BETHWEEN_DIRECTION_CHANGE = 200;
        private static final int MAX_TOTAL_DURATION_OF_SHAKE = 400;
        private long mFirstDirectionChangeTime = 0;
        private long mLastDirectionChangeTime;

        private int mDirectionChangeCount = 0;
        private float lastX = 0;
        private float lastY = 0;
        private float lastZ = 0;
        private OnShakeListener mShakeListener;

        public interface OnShakeListener {
            void onShake();
        }

        public void setOnShakeListener(OnShakeListener listener) {
            mShakeListener = listener;
        }

        @Override
        public void onSensorChanged(SensorEvent se) {
            float x = se.values[SensorManager.DATA_X];
            float y = se.values[SensorManager.DATA_Y];
            float z = se.values[SensorManager.DATA_Z];
            float totalMovement = Math.abs(x + y + z - lastX - lastY - lastZ);
            if (totalMovement > MIN_FORCE) {
                long now = System.currentTimeMillis();
                if (mFirstDirectionChangeTime == 0) {
                    mFirstDirectionChangeTime = now;
                    mLastDirectionChangeTime = now;
                }
                long lastChangeWasAgo = now - mLastDirectionChangeTime;
                if (lastChangeWasAgo < MAX_PAUSE_BETHWEEN_DIRECTION_CHANGE) {
                    mLastDirectionChangeTime = now;
                    mDirectionChangeCount++;
                    lastX = x;
                    lastY = y;
                    lastZ = z;
                    if (mDirectionChangeCount >= MIN_DIRECTION_CHANGE) {
                        long totalDuration = now - mFirstDirectionChangeTime;
                        if (totalDuration < MAX_TOTAL_DURATION_OF_SHAKE) {
                            mShakeListener.onShake();
                            resetShakeParameters();
                        }
                    }
                } else {
                    resetShakeParameters();
                }
            }
        }
        private void resetShakeParameters() {
            mFirstDirectionChangeTime = 0;
            mDirectionChangeCount = 0;
            mLastDirectionChangeTime = 0;
            lastX = 0;
            lastY = 0;
            lastZ = 0;
        }
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.