FileDocCategorySizeDatePackage
OrientationEventListener.javaAPI DocAndroid 5.1 API6301Thu Mar 12 22:22:10 GMT 2015android.view

OrientationEventListener

public abstract class OrientationEventListener extends Object
Helper class for receiving notifications from the SensorManager when the orientation of the device has changed.

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private static final boolean
localLOGV
private int
mOrientation
private android.hardware.SensorManager
mSensorManager
private boolean
mEnabled
private int
mRate
private android.hardware.Sensor
mSensor
private android.hardware.SensorEventListener
mSensorEventListener
private OrientationListener
mOldListener
public static final int
ORIENTATION_UNKNOWN
Returned from onOrientationChanged when the device orientation cannot be determined (typically when the device is in a close to flat position).
Constructors Summary
public OrientationEventListener(android.content.Context context)
Creates a new OrientationEventListener.

param
context for the OrientationEventListener.


                   
       
        this(context, SensorManager.SENSOR_DELAY_NORMAL);
    
public OrientationEventListener(android.content.Context context, int rate)
Creates a new OrientationEventListener.

param
context for the OrientationEventListener.
param
rate at which sensor events are processed (see also {@link android.hardware.SensorManager SensorManager}). Use the default value of {@link android.hardware.SensorManager#SENSOR_DELAY_NORMAL SENSOR_DELAY_NORMAL} for simple screen orientation change detection.

        mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
        mRate = rate;
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        if (mSensor != null) {
            // Create listener only if sensors do exist
            mSensorEventListener = new SensorEventListenerImpl();
        }
    
Methods Summary
public booleancanDetectOrientation()

        return mSensor != null;
    
public voiddisable()
Disables the OrientationEventListener.

        if (mSensor == null) {
            Log.w(TAG, "Cannot detect sensors. Invalid disable");
            return;
        }
        if (mEnabled == true) {
            if (localLOGV) Log.d(TAG, "OrientationEventListener disabled");
            mSensorManager.unregisterListener(mSensorEventListener);
            mEnabled = false;
        }
    
public voidenable()
Enables the OrientationEventListener so it will monitor the sensor and call {@link #onOrientationChanged} when the device orientation changes.

        if (mSensor == null) {
            Log.w(TAG, "Cannot detect sensors. Not enabled");
            return;
        }
        if (mEnabled == false) {
            if (localLOGV) Log.d(TAG, "OrientationEventListener enabled");
            mSensorManager.registerListener(mSensorEventListener, mSensor, mRate);
            mEnabled = true;
        }
    
public abstract voidonOrientationChanged(int orientation)
Called when the orientation of the device has changed. orientation parameter is in degrees, ranging from 0 to 359. orientation is 0 degrees when the device is oriented in its natural position, 90 degrees when its left side is at the top, 180 degrees when it is upside down, and 270 degrees when its right side is to the top. {@link #ORIENTATION_UNKNOWN} is returned when the device is close to flat and the orientation cannot be determined.

param
orientation The new orientation of the device.
see
#ORIENTATION_UNKNOWN

voidregisterListener(OrientationListener lis)

        mOldListener = lis;