Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Access Accelerometer in Android

DZone's Guide to

How to Access Accelerometer in Android

The following code snippet will help you with the basic understanding of how to set up the Android accelerometer and get values from it.

· Mobile Zone
Free Resource

Get gorgeous, multi-touch charts for your iOS application with just a few lines of code.

To use the accelerometer (or any sensor in general) your class should implement the SensorEventListener interface, or you could do anonymous inner classes for them. To access the accelerometer you will need to get the SystemManager from the system and get a sensors list from that.

myManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
// this will return a list of Sensor
sensors = myManager.getSensorList(Sensor.TYPE_ACCELEROMETER);

From the sensors list then select the first element, this should be the accelerometer sensor.
The parameter of the event listener is a SensorEvent. From this event you can access the x, y, and z values of the accelerometer and do with them what you want.

calculateSomething(event.values[0], event.values[1], event.values[2]);

.Net developers: use Highcharts, the industry's leading interactive charting library, without writing a single line of JavaScript.

Topics:
android ,accelerometer

Published at DZone with permission of Nilanchala Panigrahy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}