Question

I am facing a challenge in my new application, i want to find the android phone screen facing ceiling or floor.Can i able to make this with the android sensors.If any body tried this please share your valuable ideas and tutorial if available.

Was it helpful?

Solution

In oncreate

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    List<Sensor> sensorList = sensorManager
            .getSensorList(Sensor.TYPE_ACCELEROMETER);
    if (sensorList.size() > 0) {
        accelerometerPresent = true;
        accelerometerSensor = sensorList.get(0);
    }

register the SensorEventListener

        sensorManager.registerListener(accelerometerListener,
                accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);

and the SensorEventListener implementation

private SensorEventListener accelerometerListener = new SensorEventListener() {

    public void onAccuracyChanged(Sensor sensor, int accuracy) {            
    }

    public void onSensorChanged(SensorEvent event) {
        float z_value = event.values[2];
        if (z_value == 10) {
            // up facing
        } else if(z_value == -10)
            // down facing
        }           
    }
};

In my two device it shows z_values = 10 when it is upfacing and horizontal and in the opposite case it is showing - 10. check your cases.

OTHER TIPS

You can use the SensorEventListener to achieve this. Have a look at my below code for a better understanding. You can also look at this in the developer docs for more about sensors.

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class SensorClass implements SensorEventListener {
    private float   mInR[];
    private float   mI[];
    private float   mGravity[];
    private float   mGeomag[];
    private float   mOrientVals[];

    final float rad2deg = 180/((float) Math.PI);

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Do something here when accuracy changed
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        float azimuth = 0f;
        float pitch = 0f;
        float roll = 0f;

        // If the sensor data is unreliable return
        if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
            return;

        // Gets the value of the sensor that has been changed
        switch (event.sensor.getType()){  
        case Sensor.TYPE_ACCELEROMETER:
            mGravity = event.values.clone();
            break;
        case Sensor.TYPE_MAGNETIC_FIELD:
            mGeomag = event.values.clone();
            break;
        }

         if (mGravity != null && mGeomag != null){
            // checks that the rotation matrix is found
            if (SensorManager.getRotationMatrix(mInR, mI, mGravity, mGeomag)){

                // magnetic north and the horizon respectively
                SensorManager.getOrientation(mInR, mOrientVals);

                azimuth = mOrientVals[0]*rad2deg;
                pitch = mOrientVals[1]*rad2deg;
                roll = mOrientVals[2]*rad2deg;
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top