41

I'm trying to build an app for reading the values from the accelerometer on my phone, which supports Android 2.1 only.

How do I read from the accelerometer using 2.1-compatible code?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
user642866
  • 419
  • 1
  • 4
  • 3

3 Answers3

53

Start with this:

public class yourActivity 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];
            }
   }
}
ArcDare
  • 3,106
  • 4
  • 27
  • 38
34

This isn't easily explained in a few paragraphs. You should try to read:

These show a framework on how to access sensors:

 public class SensorActivity extends Activity implements SensorEventListener {
     private final SensorManager mSensorManager;
     private final Sensor mAccelerometer;

     public SensorActivity() {
         mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
         mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
     }

     protected void onResume() {
         super.onResume();
         mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
     }

     protected void onPause() {
         super.onPause();
         mSensorManager.unregisterListener(this);
     }

     public void onAccuracyChanged(Sensor sensor, int accuracy) {
     }

     public void onSensorChanged(SensorEvent event) {
     }
 }

In the onSensorChanged callback you can query the sensor's values through the SensorEvent.

slhck
  • 36,575
  • 28
  • 148
  • 201
  • 1
    The server on AndroGames doesn't seem to exist anymore, and the AndDev link is from 2008 = horribly old. The other suggestions are good though. – Simon Forsberg Sep 02 '12 at 22:36
  • Ah, well it's been a while since I've posted that answer. Thanks for bringing that to my attention. – slhck Sep 03 '12 at 06:42
  • I would recommend to remove any reference to AndDev website -it is sending high-severity cyber attack intercepted by Norton. Thanks for the understanding. Best regards, – Alexander Bell Nov 26 '17 at 05:17
  • 1
    @AlexBell I can't verify your claim, but anyway, it's a post from 2008, so I removed those links. – slhck Nov 26 '17 at 11:03
  • Thanks for the prompt response. Best regards, – Alexander Bell Nov 26 '17 at 14:51
7

A very good example for accelerometer app.

http://www.techrepublic.com/blog/app-builder/a-quick-tutorial-on-coding-androids-accelerometer/472

Pratap
  • 79
  • 1
  • 1
  • Working link: https://www.techrepublic.com/article/a-quick-tutorial-on-coding-androids-accelerometer/ [edit queue is full or I'd submit it as such] – belkarx Feb 17 '22 at 02:08