Search code examples
javaandroidandroid-sensorsandroid-orientationdevice-orientation

Android - How to know when device is flat to ground or not?


I want to know if a device is flat to the ground or not?

Flat Mode:

My Android device is placed flat on a table with its screen upside. Then it is laying flat to the ground.

Not Flat:

And when I picks up the device from its top corner - keeping its bottom corner still on table. Then it starts standing vertical to the ground. And Now it is not lying flat to the ground anymore.

Problems:

How can I know exactly using sensors and SensorEvents in android about this? TYPE_ORIENTATION is now deprecated. But still how can specific sensor values be used to determine if the device is flat to the ground or not. Where would I find such calculation?

I have explored some posts about sensors but I am still confused how my problem is going to get solved?

Related Posts:

How should I calculate azimuth, pitch, orientation when my Android device isn't flat?

Android Compass that can Compensate for Tilt and Pitch


Solution

  • You have to register for sensor type accelerometer and then see how to calculate the tilt at How to measure the tilt of the phone in XY plane using accelerometer in Android

    For your purpose if the inclination in the above link is not zero (give or take a few degrees) then your device is lifted up.