Android device orientation without geomagnetic

2019-02-08 14:09发布

I need to get device orientation. As I know usually used TYPE_ACCELEROMETER and TYPE_MAGNETIC_FIELD sensors. My problem is that SensorManager.getDefaultSensor returns me null for geomagnetic sensor. It returns null for TYPE_ORIENTATION sensor too.

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);

I need another ways to get device orientation.

7条回答
何必那么认真
2楼-- · 2019-02-08 15:05

To get the angle of rotation when the device is not flat, implements OrientationEventListener. onOrientationChanged will give the device orientation. See https://developer.android.com/reference/android/view/OrientationEventListener.html for detail.

查看更多
登录 后发表回答