Wednesday, June 29, 2011

Android Sensor - Tutorial


Lars Vogel

Version 0.2
14.06.2011
Revision History
Revision 0.105.01.2011Lars Vogel
Created
Revision 0.214.06.2011Lars Vogel
bugfixes and enhancements
Android Sensor
This tutorial describes how to use the Androids Sensor manager. It currently demonstrates the accelerometer. The tutorial is based on Eclipse 3.6, Java 1.6 and Android 2.3.3 (Gingerbread).

1. Android Sensors

Android supports several sensors via the SensorManager, for example the accelerometer. Unfortunately you cannot test the accelerometer on the Android emulator.
Once you get the ServiceManager via getSystemService(SENSOR_SERVICE) you can register a "SensorEventListener" to it. To avoid the unnecessary usage of battery you register your listener in the onResume method and de-register on the onPause method.

2. Example

Create a new Android project "de.vogella.android.sensor" with the activity "SensorTest".
Change your layout "main.xml" to the following.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView android:id="@+id/textView" android:layout_width="match_parent"
  android:layout_height="match_parent" android:text="Shake to get a toast and to switch color" />
</LinearLayout>

  

Change the coding of your activity Activity.

package de.vogella.android.sensor;

import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class SensorTest extends Activity implements SensorEventListener {
 private SensorManager sensorManager;
 private boolean color = false; 
 private View view;
 private long lastUpdate;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
             WindowManager.LayoutParams.FLAG_FULLSCREEN);
   
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  view = findViewById(R.id.textView);
  view.setBackgroundColor(Color.GREEN);

  sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  sensorManager.registerListener(this,
    sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
    SensorManager.SENSOR_DELAY_NORMAL);
  lastUpdate = System.currentTimeMillis();
 }

 @Override
 public void onSensorChanged(SensorEvent event) {
  if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
   float[] values = event.values;
   // Movement
   float x = values[0];
   float y = values[1];
   float z = values[2];

   float accelationSquareRoot = (x * x + y * y + z * z)
     / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
   long actualTime = System.currentTimeMillis();
   if (accelationSquareRoot >= 2) //
   {
    if (actualTime - lastUpdate < 200) {
     return;
    }
    lastUpdate = actualTime;
    Toast.makeText(this, "Device was shuffed", Toast.LENGTH_SHORT)
      .show();
    if (color) {
     view.setBackgroundColor(Color.GREEN);
     
    } else {
     view.setBackgroundColor(Color.RED);
    }
    color = !color;
   }

  }

 }

 @Override
 public void onAccuracyChanged(Sensor sensor, int accuracy) {
  // TODO Auto-generated method stub

 }

 @Override
 protected void onResume() {
  super.onResume();
  // register this class as a listener for the orientation and
  // accelerometer sensors
  sensorManager.registerListener(this,
    sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
    SensorManager.SENSOR_DELAY_NORMAL);
 }

 @Override
 protected void onPause() {
  // unregister listener
  sensorManager.unregisterListener(this);
  super.onStop();
 }
}
  

3. Thank you

No comments:

Post a Comment