This is a simple post showing how to use accelerometer in android.

This java code prints the values of x,y and z axis in the console.

package com.coderzheaven.pack;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;

public class AccelerometerTest extends Activity  implements SensorEventListener{
	 private SensorManager sensorManager;
	 double ax,ay,az;   // these are the acceleration in x,y and z axis

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	 super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
         sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
    }
    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
         if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
             		 ax=event.values[0];
                     ay=event.values[1];
                     az=event.values[2];
                     System.out.println("X = " + ax + ", Y = " + ay +", Z = " + az);
             }
    }
 }

Leave your comments on this post.
Find Coderzheaven on facebook, twitter , google Plus and MySpace for more updates.

 

2 Comments

  1. Pingback: How to use Accelerometer in Android? « Emerson Barros

  2. Android Example October 7, 2013 at 11:53 am

    Very nice dude…

    I have also found one good link here….

    Accelerometer Basic Example – Detect Phone Shake Motion

     

Leave a Reply

Your email address will not be published. Required fields are marked *

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.