Version 0.2
Copyright © 2010 Lars Vogel
14.06.2011
Revision History | ||
---|---|---|
Revision 0.1 | 05.01.2011 | Lars Vogel |
Created | ||
Revision 0.2 | 14.06.2011 | Lars Vogel |
bugfixes and enhancements |
Table of Contents
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.
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.
Create a new Android project "de.vogella.android.sensor" with the activity "SensorTest".
Change your layout "main.xml" to the following.
Change the coding of your activity Activity.
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(); } }
No comments:
Post a Comment