我需要获得设备导向.我知道通常使用TYPE_ACCELEROMETER和TYPE_MAGNETIC_FIELD传感器.我的问题是SensorManager.getDefaultSensor为地磁传感器返回null.它也为TYPE_ORIENTATION传感器返回null.
manager = (SensorManager) getSystemService(SENSOR_SERVICE); Sensor sensorAcc = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),//normal object sensorMagn = manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); //null orientationListener = new OrientationSensorListener(); manager.registerListener(orientationListener,sensorAcc,10); manager.registerListener(orientationListener,sensorMagn,10);
解决方法
要获得设备不平坦时的旋转角度,请执行OrientationEventListener. onOrientationChanged将给出设备方向.详见
https://developer.android.com/reference/android/view/OrientationEventListener.html.