传感器

作者: 初昇陌 | 来源:发表于2019-03-19 20:21 被阅读0次

    1.实现SensorEventListener接口

    public class MainActivity extends AppCompatActivity implements SensorEventListener{
    }
    

    2.activity中执行

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
            //获取对象
            manager = (SensorManager) getSystemService(SENSOR_SERVICE);
            List<Sensor> sensorList = manager.getSensorList(Sensor.TYPE_ALL);
            //log中打印所有传感器
            for (int i = 0; i < sensorList.size(); i++) {
                Sensor sensor = sensorList.get(i);
                Log.e(TAG, "onCreate: " + sensor.getName());
            }
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            //取消注册
            manager.unregisterListener(this);
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            //  在start中注册传感器
            //加速度传感器
            manager.registerListener(this, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                    SensorManager.SENSOR_DELAY_NORMAL);
            //光感传感器
            manager.registerListener(this, manager.getDefaultSensor(Sensor.TYPE_LIGHT),
                    SensorManager.SENSOR_DELAY_NORMAL);
            //方向传感器
            manager.registerListener(this, manager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                    SensorManager.SENSOR_DELAY_NORMAL);
    
        }
    
        @Override
        public void onSensorChanged(SensorEvent event) {
            Sensor sensor = event.sensor;
            if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                float x = event.values[0];
                float y = event.values[1];
                float z = event.values[2];
                txt_x1.setText("x轴加速度" + x);
                txt_y1.setText("y轴加速度" + y);
                txt_z1.setText("z轴加速度" + z);
                //摇一摇如果满足产生震动
                if (Math.abs(x) > 15 || Math.abs(y) > 15 || Math.abs(z) > 15) {
                    share();
                }
            } else if (sensor.getType() == Sensor.TYPE_LIGHT) {
                float value = event.values[0];
                light.setText("光线强度:"+value);
            } else if (sensor.getType() == Sensor.TYPE_ORIENTATION) {
                float z = event.values[0];
                float x = event.values[1];
                float y = event.values[2];
                txt_z2.setText("绕z轴转过的角度:"+z);
                txt_y2.setText("绕x轴转过的角度:"+y);
                txt_x2.setText("绕y轴转过的角度:"+x);
            }
    
        private void share() {
            //带有声音
            MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.weichat_audio);
            mediaPlayer.start();
    
            vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
            //设置循环奇数为震动时间
            long[] patter = {1000, 2000, 2000, 1000};
            不为-1时一直震动
            vibrator.vibrate(patter, -1);
        }
    
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
    
        }
    

    相关文章

      网友评论

          本文标题:传感器

          本文链接:https://www.haomeiwen.com/subject/sfznmqtx.html