Sensor Fun: Using the accelerometer on Android
April 27th, 2010 by Micha KopsHere is an example on how to use the accelerometer in your Android application. If you want to simulate the acceleration on the emulator I highly recommend the Sensor Simulator on the OpenIntents website.
The following example app displays the coordinates received by the sensor.
The Acceleration App
- The activity – AccelerationActivity.java
package com.hascode.android; 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 AccellerationActivity extends Activity { private TextView result; private SensorManager sensorManager; private Sensor sensor; private float x, y, z; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); sensor = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); result = (TextView) findViewById(R.id.result); result.setText("No result yet"); } private void refreshDisplay() { String output = String .format("x is: %f / y is: %f / z is: %f", x, y, z); result.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) { x = event.values[0]; y = event.values[1]; z = event.values[2]; refreshDisplay(); } }; }
The Application Layout
- A simple layout defined in the main.xml
<?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/result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> </LinearLayout>
- The strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, AccellerationActivity!</string> <string name="app_name">Accelerometer Snippet</string> </resources>
Google App Inventor
*update* If you like to see if this application is developed using Google’s App Inventor then take a look at the article “How to create an Android App using Google’s App Inventor”
Resources
- Android Sensor API
- OpenIntents: Sensor Simulator
- Stuffthathappens.com: Android Accelerometer
- IBM.com: Tapping into Android’s Sensors
Tags: accelerometer, Android, Api, sensor
November 24th, 2010 at 8:11 am
Are there no ways to get acceleration values without EventListener?
November 24th, 2010 at 7:25 pm
imho there is currently no other way. if you find one – please tell me :)
May 22nd, 2011 at 7:41 am
Your code is amazing, tiny and explicit, by the way i need connect my cel phone whit pc yet.
Tanxs.
January 8th, 2012 at 10:30 am
Your code is very helpfull ..Its simple and adaptive..
February 3rd, 2012 at 7:19 am
Thanks for publishing small but accurate tutorial!!