How to detect movement of an android device?

后端 未结 6 1490
日久生厌
日久生厌 2020-12-04 09:51

I need suggestion about how to detect the amount of movement of an android device. Suppose I have put the phone on a table or bed and then if somebody taps the table or sits

6条回答
  •  庸人自扰
    2020-12-04 10:35

    I used the following class:

    public class MovementDetector implements SensorEventListener {
    
    protected final String TAG = getClass().getSimpleName();
    
    private SensorManager sensorMan;
    private Sensor accelerometer;
    
    private MovementDetector() {
    }
    
    private static MovementDetector mInstance;
    
    public static MovementDetector getInstance() {
        if (mInstance == null) {
            mInstance = new MovementDetector();
            mInstance.init();
        }
        return mInstance;
    }
    
    //////////////////////
    private HashSet mListeners = new HashSet();
    
    private void init() {
        sensorMan = (SensorManager) GlobalData.getInstance().getContext().getSystemService(Context.SENSOR_SERVICE);
        accelerometer = sensorMan.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
    }
    
    public void start() {
        sensorMan.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }
    
    public void stop() {
        sensorMan.unregisterListener(this);
    }
    
    public void addListener(Listener listener) {
        mListeners.add(listener);
    }
    
    /* (non-Javadoc)
     * @see android.hardware.SensorEventListener#onSensorChanged(android.hardware.SensorEvent)
     */
    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
    
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];
    
            float diff = (float) Math.sqrt(x * x + y * y + z * z);
            if (diff > 0.5) // 0.5 is a threshold, you can test it and change it
                Log.d(TAG,"Device motion detected!!!!");
            for (Listener listener : mListeners) {
                listener.onMotionDetected(event, diff);
            }
        }
    
    }
    
    /* (non-Javadoc)
     * @see android.hardware.SensorEventListener#onAccuracyChanged(android.hardware.Sensor, int)
     */
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub
    
    }
    
    public interface Listener {
        void onMotionDetected(SensorEvent event, float acceleration);
        }
    }
    

    Usage:

    On my activity onCrate():

            MovementDetector.getInstance().addListener(new MovementDetector.Listener() {
    
            @Override
            public void onMotionDetected(SensorEvent event, float acceleration) {
    
                mMotionDetectionTextView.setText("Acceleration: ["+String.format("%.3f",event.values[0])+","+String.format("%.3f",event.values[1])+","+String.format("%.3f",event.values[2])+"] "+String.format("%.3f", acceleration));
                if (acceleration > SettingsHelper.getInstance().getMotionDetectionThreshold()){
                    mMotionDetectionTextView.setTextColor(Color.RED);
                } else {
                    mMotionDetectionTextView.setTextColor(Color.WHITE);
                }
    
            }
        });
    

    On my activity onResume():

    MovementDetector.getInstance().start();
    

    On my activity onPause():

    MovementDetector.getInstance().stop();
    

提交回复
热议问题