Search code examples
androidandroid-sensors

How to read orientation of Android Phone


I want to read orientation of the phone. However when I use

mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

It shows a warning The field Sensor.TYPE_ORIENTATION is deprecated.

I also have a warning when reading the gyroscope value.

Could someone help me to read these two data. Thanks in advance.


Solution

  • The current configuration, as used to determine which resources to retrieve etc, as available from the Resources' Configuration object as:

    getResources().getConfiguration().orientation
    

    http://developer.android.com/reference/android/content/res/Configuration.html#orientation

    or

        mWindowManager =  (WindowManager) getSystemService(WINDOW_SERVICE);
        mDisplay = mWindowManager.getDefaultDisplay();
    
        Log.d("ORIENTATION_TEST", "getOrientation(): " + mDisplay.getOrientation());