Feb 11, 2013

Detect device orientation by gravity sensor

The code demonstrate how to detect device orientation, face-up/face-down, base on gravity sensor data.

Detect device orientation by gravity sensor
Detect device orientation by gravity sensor


We can determine the device orientation(face-up or face-down) from Z value of Gravity sensor. The constant SensorManager.STANDARD_GRAVITY store the value 9.80665, it's the standard gravity (g) on Earth, equivalent to 1G. We can compare Z value with it to determine device orientation.

The code register SensorEventListener for Sensor.TYPE_GRAVITY in onResume() callback method. Once Gravity Sensor changed, the Z value can be retrieved from event.values[2].
Example code:
package com.example.androidgravitysensor;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity implements SensorEventListener{
 
 private SensorManager mySensorManager;
 private Sensor myGravitySensor;
 
 TextView textFace, textZValue, textStandardGravity, textThreshold;
 
 float standardGravity;
 float thresholdGraqvity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textFace = (TextView)findViewById(R.id.face);
        textZValue = (TextView)findViewById(R.id.zvalue);
        textStandardGravity = (TextView)findViewById(R.id.standardgravity);
        textThreshold = (TextView)findViewById(R.id.threshold);
        
        standardGravity = SensorManager.STANDARD_GRAVITY;
        thresholdGraqvity = standardGravity/2;
        
        textFace.setText("");
        textZValue.setText("");
        textStandardGravity.setText("Standard Gravity = " + standardGravity);
        textThreshold.setText("Threshold = " + thresholdGraqvity);
        
        mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        myGravitySensor = mySensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
        

    }

 @Override
 public void onAccuracyChanged(Sensor arg0, int arg1) {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void onSensorChanged(SensorEvent event) {
  Sensor source = event.sensor;
  float z = event.values[2];
  
  if(source.getType() == Sensor.TYPE_GRAVITY){
   
   textZValue.setText("Z value = " + z);
   
   if (z >= thresholdGraqvity){
    textFace.setText("Face UP");
   }else if(z <= -thresholdGraqvity){
    textFace.setText("Face DOWN");
   }else{
    textFace.setText("");
   }
  }
  
 }

 @Override
 protected void onPause() {
  super.onPause();
  mySensorManager.unregisterListener(this);
  
 }

 @Override
 protected void onResume() {
  super.onResume();
  mySensorManager.registerListener(
    this, 
    myGravitySensor, 
    SensorManager.SENSOR_DELAY_NORMAL);
 }

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <TextView
        android:id="@+id/face"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/zvalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/standardgravity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/threshold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


Related article: Get details of gravity sensor

No comments:

Post a Comment

Infolinks In Text Ads