Version 0.4
Copyright © 2011, 2012 Lars Vogel
07.03.2012
| Revision History | |||
|---|---|---|---|
| Revision 0.1 | 05.01.2011 | Lars Vogel |
Created |
| Revision 0.2 - 0.4 | 14.06.2011 - 07.03.2012 | Lars Vogel |
bugfixes and enhancements |
Table of Contents
Sensors
Android supports several sensors via the
SensorManager, for
example the accelerometer. Unfortunately you cannot test the
accelerometer on the Android emulator.
You can access a
SensorManager
via
getSystemService(SENSOR_SERVICE). The
Sensor
class defines several constants for accessing the different sensors.
Sensor.TYPE_GYROSCOPE
Sensor.TYPE_MAGNETIC_FIELD
Sensor.TYPE_ORIENTATION
Sensor.TYPE_ACCELEROMETER
You can access the Sensor via the
sensorManager.getDefaultSensor()
method, which take as parameters the sensor type and the deplay
defined as constants on
SensorManager.
Once you acquired a sensor, you
can register a
SensorEventListener
object
on it. This listener will get informed, if the sensor data
changes.
To
avoid the unnecessary usage of battery
you register your
listener in
the
onResume()
method and de-register it in
the
onPause()
method.
We will build an application which will change its background color, if it is shuffled. Create a new Android project called de.vogella.android.sensor with an activity called SensorTestActivity.
Change your layout file to the following code.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <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 your activity class to the following code.
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 SensorTestActivity 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); lastUpdate = System.currentTimeMillis(); } @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { getAccelerometer(event); } } private void getAccelerometer(SensorEvent event) { 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) { } @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 super.onPause(); sensorManager.unregisterListener(this); } }
Create a new Android project called de.vogella.android.sensor.compass with an activity called MainActivity.
Create the following custom
View
class.
package de.vogella.android.sensor.compass; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View; public class MyCompassView extends View { private Paint paint; private float position = 0; public MyCompassView(Context context) { super(context); init(); } private void init() { paint = new Paint(); paint.setAntiAlias(true); paint.setStrokeWidth(2); paint.setTextSize(25); paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.WHITE); } @Override protected void onDraw(Canvas canvas) { int xPoint = getMeasuredWidth() / 2; int yPoint = getMeasuredHeight() / 2; float radius = (float) (Math.max(xPoint, yPoint) * 0.6); canvas.drawCircle(xPoint, yPoint, radius, paint); canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), paint); // 3.143 is a good approximation for the circle canvas.drawLine(xPoint, yPoint, (float) (xPoint + radius * Math.sin((double) (-position) / 180 * 3.143)), (float) (yPoint - radius * Math.cos((double) (-position) / 180 * 3.143)), paint); canvas.drawText(String.valueOf(position), xPoint, yPoint, paint); } public void updateData(float position) { this.position = position; invalidate(); } }
Change the coding of your activity.
package de.vogella.android.sensor.compass; 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.util.Log; import android.widget.Toast; public class MainActivity extends Activity { private static SensorManager sensorService; private MyCompassView compassView; private Sensor sensor;/** Called when the activity is first created. */@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); compassView = new MyCompassView(this); setContentView(compassView); sensorService = (SensorManager) getSystemService(Context.SENSOR_SERVICE); sensor = sensorService.getDefaultSensor(Sensor.TYPE_ORIENTATION); if (sensor != null) { sensorService.registerListener(mySensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL); Log.i("Compass MainActivity", "Registerered for ORIENTATION Sensor"); } else { Log.e("Compass MainActivity", "Registerered for ORIENTATION Sensor"); Toast.makeText(this, "ORIENTATION Sensor not found", Toast.LENGTH_LONG).show(); finish(); } } private SensorEventListener mySensorEventListener = new SensorEventListener() { @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override public void onSensorChanged(SensorEvent event) { // angle between the magnetic north directio // 0=North, 90=East, 180=South, 270=West float azimuth = event.values[0]; compassView.updateData(azimuth); } }; @Override protected void onDestroy() { super.onDestroy(); if (sensor != null) { sensorService.unregisterListener(mySensorEventListener); } } }
Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.
vogella Training Android and Eclipse Training from the vogella team
Android Tutorial Introduction to Android Programming
GWT Tutorial Program in Java and compile to JavaScript and HTML
Eclipse RCP Tutorial Create native applications in Java
JUnit Tutorial Test your application
Git Tutorial Put everything you have under distributed version control system