android 通过加速度传感器实现手机来电时翻转静音

实现思路

  1. 通过加速度传感器获取 X,Y,Z 三轴所受的重力
  2. 计算在指定的时间段内,手机在 X,Y,Z 矢量和的方向上的速度
  3. 如果速度大于指定的速度阀值,判定手机当前在剧烈抖动;并结束当前方法
  4. 通过Z轴的改变判断手机是否发生反转,如果是调用AudioManager的setRingerMode(AudioManager.RINGER_MODE_SILENT)方法使手机静音

代码

public class MainActivity extends AppCompatActivity {
    SensorManager mSensorManager = null;
    AccelerometerSilentListener mAccelerometerSilentListener = null;
    AudioManager mAudioManager = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mAccelerometerSilentListener = new AccelerometerSilentListener();
        mSensorManager.registerListener(mAccelerometerSilentListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mSensorManager != null) {
            mSensorManager.unregisterListener(mAccelerometerSilentListener);
        }
    }


    private class AccelerometerSilentListener implements SensorEventListener {
        private static final int SPEED_LIMIT = 800; //速度阀指
        private static final int UPDATE_INTERNAL_TIME = 60; //两次取样时间差
        private long lastTime = 0;

        private float lastX = 0;
        private float lastY = 0;
        private float lastZ = 0;

        private boolean isUp = false;

        @Override
        public void onSensorChanged(SensorEvent event) {
            long currentTime = System.currentTimeMillis();
            float internalTime = currentTime - lastTime;
            if (internalTime < UPDATE_INTERNAL_TIME) {
                return;
            }
            lastTime = currentTime;

            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];
            float deltaX = x - lastX;
            float deltaY = y - lastY;
            float deltaZ = z - lastZ;
            lastX = x;
            lastY = y;
            lastZ = z;

            double speed = Math.sqrt(deltaX*deltaX + deltaY*deltaY + deltaZ*deltaZ) / internalTime * 10000; //算出后速度为 mm/s
            if (speed > SPEED_LIMIT) {
                return;
            }

            if (x > -1 && x < 1 && y > -1 && y < 1) {
                if (z > 0) {
                    isUp = true;
                } else {
                    isUp = false;
                }
            } else {
                if (z > -10f && z < -9.6f && isUp) {
                    mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                } else if (z > 9.6f && z < 10f && !isUp) {
                    mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                }
            }
        }

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

        }
    }

}

你可能感兴趣的:(android进阶)