Code_Yoga
Code_Yoga

Reputation: 3248

Android : Orientation : Find out if a device supports orientations change

I would like to know if an Android phone actually has an Orientation Sensor. I doubt not all android phones screen changes with change in Orientation (like phones with qwerty keypad)

I have gone through the documentation and found a boolean method 'canDetectOrientation ()' here But I found nothing mentioned in the description. Can anybody tell me if this is the method to find out if a device supports orientation changes .

Thanks in advance.

Upvotes: 1

Views: 1690

Answers (2)

rajpara
rajpara

Reputation: 5203

If we pass Type.All in getSensorList function then we will get list of all available sensor.

Then we can traverse a list and fetch Sensor type of all sensor if we found Sensor type Orientation (integer value is 3) then we can conclude that that device has that sensor.

Below is code snippet for the same.

boolean OrientationSensorFound=false;
SensorManager mSensorManager;
Sensor mSensor;

mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
List<Sensor> mSensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL );

for(int i=0;i<mSensorList.size();i++){
    // 3 is value for Orientation sensor
    if(Sensor.TYPE_ORIENTATION==mSensorList.get(i).getType()){
        OrientationSensorFound=true;
        break;
    }
}
Log.i("Sensor Found", "Orientation found : "+OrientationSensorFound);

Upvotes: 2

Related Questions