Tuesday 3 September 2013

Android Accelerometer Sensors Sample Code

The Android platform supports three broad categories of sensors:

  • Motion sensors These sensors measure acceleration forces and rotational forces along three axes. This category includes accelerometers, gravity sensors, gyroscopes, and rotational vector sensors.
  • Environmental sensors These sensors measure various environmental parameters, such as ambient air temperature and pressure, illumination, and humidity. This category includes barometers, photometers, and thermometers.
  • Position sensors These sensors measure the physical position of a device. This category includes orientation sensors and magnetometers.

The accelerometer is a hardware sensor in android  used to detect a motion,
Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), including the force of gravity.

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

public class MainActivity extends Activity 
{
    private SensorManager sensorManager;
    private Sensor sensor;
    private int x, y, z;
    TextView tv;
   
    /** Called when the activity is first created. */
 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.textView1);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
    }
   
    private void refreshDisplay()
    {
        String output = String
                .format("x is: %d / y is: %d / z is: %d", x, y, z);
        tv.setText(output);
    }
   
    @Override
    protected void onResume()
    {
        super.onResume();
        sensorManager.registerListener(accelerationListener, sensor,
                SensorManager.SENSOR_DELAY_GAME);
    }
  
    @Override
    protected void onStop()
    {
        sensorManager.unregisterListener(accelerationListener);
        super.onStop();
      
    }

   
    private SensorEventListener accelerationListener = new SensorEventListener()
    {
        @Override
        public void onAccuracyChanged(Sensor sensor, int acc)
        {
        }

      
        @Override
        public void onSensorChanged(SensorEvent event) {
            // TODO Auto-generated method stub
            x = (int) event.values[0];
            y = (int) event.values[1];
            z = (int) event.values[2];
          
            refreshDisplay();
        }
         };
      
} 

Create a layout

<RelativeLayout 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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
 

 

No comments:

Post a Comment