Android——重力感应与加速度计的使用

传感器

https://developer.android.com/guide/topics/sensors/sensors_overview#java

  1. 确定设备上可用的传感器
  2. 确定单个传感器的功能(范围、分辨率)
  3. 获取原始传感器数据并定义获取传感器数据的最少速率
  4. 注册和取消注册监视传感器更改的传感器事件监听器

传感器类型

TYPE :

  1. TYPE_ACCELEROMETER 加速度计
  2. TYPE_GRAVITY 重力感应

其中参数 :链接

具体使用

共四步

  • 获取 Manager
    SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  • 通过 Manager 获取 Sensor
    见下文代码
  • 注册 registerListener
    见下文代码
  • 监听 new SensorEventListener
    在接口中可以获取传感器数据并处理
代码中的 X Y Z

需求:判断手机是否抖动

实例代码:

public class MainActivity extends AppCompatActivity {


    private TextView axisX;
    private TextView axisY;
    private TextView axisZ;
    private String TAG = "sensor";
    private SensorManager sensorManager;
    private SensorEventListener gyroscopeSensorListener;
    private Sensor defaultSensor;
    private float X;
    private float Y;
    private float Z;


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

        //开始检测

        starDetection();
        // starRotatio();
    }

    @Override
    protected void onResume() {
        super.onResume();


    }


    @Override
    protected void onStop() {
        sensorManager.unregisterListener(gyroscopeSensorListener,defaultSensor);

        super.onStop();

    }

    private void starDetection() {
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        defaultSensor = null;
        if (sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY) != null){
            List sensorList = sensorManager.getSensorList(Sensor.TYPE_GRAVITY);
            for (int i = 0; i < sensorList.size(); i++) {
                if ((sensorList.get(i).getVendor().contains("Google LLC")) &&
                (sensorList.get(i).getVersion() == 3)){
                    defaultSensor =  sensorList.get(i);
                }
            }
            Log.i(TAG,"TYPE_GRAVITY");
        }

        if (defaultSensor == null){
            if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null){
                defaultSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            }else {
                Log.i(TAG,"can not do task ");
            }
            Log.i(TAG,"TYPE_ACCELEROMETER");
        }
        //判断是否有传感器存在 null 为不存在
      //  defaultSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
       // float resolution = defaultSensor.getResolution();
      //  Log.i(TAG,"resolution :"+resolution);

        X = 0;
        Y = 0;
        Z = 0;


        gyroscopeSensorListener = new SensorEventListener() {

            @Override
            public void onSensorChanged(SensorEvent sensorEvent) {
                Log.d(TAG, "onSensorChanged :X =" + sensorEvent.values[0] + ",Y= " + sensorEvent.values[1]+",Z= " + sensorEvent.values[2]);


               /* sensorEvent.values[0];
                sensorEvent.values[1];
                sensorEvent.values[2];*/

                if (Z != 0){
                    if (Math.abs(sensorEvent.values[0] - X)>1 || Math.abs(sensorEvent.values[1] -Y) >1|| Math.abs(sensorEvent.values[2] -Z)>1){
                        Log.i(TAG,"================================");
                    }
                }else {

                }

                X = sensorEvent.values[0];
                Y = sensorEvent.values[1];
                Z = sensorEvent.values[2];


            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
                Log.d(TAG, "sensor: " + sensor + ", accuracy" + accuracy);

            }
        };

        sensorManager.registerListener(gyroscopeSensorListener,
                defaultSensor, SensorManager.SENSOR_DELAY_UI);//增加延时时间

    }
}

你可能感兴趣的:(Android——重力感应与加速度计的使用)