2018年4月6日 星期五

Detect device orientation by OrientationEventListener

Reference:
http://blog.csdn.net/zxccxzzxz/article/details/52282256

Code snippet:
private int mLastRotation = -1;
mOrientationEventListener = new OrientationEventListener(this,
        SensorManager.SENSOR_DELAY_NORMAL) {
    @Override    public void onOrientationChanged(int orientation) {
        if (orientation == ORIENTATION_UNKNOWN) {
            return;
        }
        Display display = windowManager.getDefaultDisplay();
        int rotation = display.getRotation();
        if (mLastRotation == DEFAULT_ROTATION){
            mLastRotation = rotation;
        } else if (rotation != mLastRotation) {
            mLastRotation = rotation;
            // notify orientation change
        }
    }
};

沒有留言: