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?
Accelerometers can be used to measure vibration on cars, machines, buildings, process control systems and safety installations. They can also be used to measure seismic activity, inclination, machine vibration, dynamic distance and speed with or without the influence of gravity.
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. sensors = myManager.
The accelerometer detects the orientation of your phone and measures its linear acceleration of movement. To check if it's working properly, tap on either "Image Test" to check for landscape-to-portrait transitions, or "Graph" to see how well the sensor detects movement by shaking your device up and down.
Accelerometers in mobile phones are used to detect the orientation of the phone. The gyroscope, or gyro for short, adds an additional dimension to the information supplied by the accelerometer by tracking rotation or twist.
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]; } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With