I am trying to get the phone angles by using TYPE_ACCELEROMETER sensor. My goal is to get the angle values only after the phone is tilted. It works, but the problem is when I put my phone facing up on the table, it still says isLandscape = true;
private boolean isLandscape;
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(
Sensor.TYPE_ACCELEROMETER),1000000);
private final SensorEventListener mSensorListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent mSensorEvent) {
float X_Axis = mSensorEvent.values[0];
float Y_Axis = mSensorEvent.values[1];
double angle = Math.atan2(X_Axis, Y_Axis)/(Math.PI/180);
if(!isLandscape) {
if(angle > 80) {
Orientation = 90;
isLandscape = true;
}
}
else
{
if(Math.abs(angle) < 10) {
Orientation = 0; //portrait
isLandscape = false;
}
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
What's the best way to get the phone angles only after the phone is tilted? I am sorry for bad English,
Thank you.