Displaying Accelerometer Values in Android
27,095
Solution 1
Have you checked out the Accelerometer example in the android samples? You could always try something like this (which I got from here):
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) {
//Right in here is where you put code to read the current sensor values and
//update any views you might have that are displaying the sensor information
//You'd get accelerometer values like this:
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
return;
float mSensorX, mSensorY;
switch (mDisplay.getRotation()) {
case Surface.ROTATION_0:
mSensorX = event.values[0];
mSensorY = event.values[1];
break;
case Surface.ROTATION_90:
mSensorX = -event.values[1];
mSensorY = event.values[0];
break;
case Surface.ROTATION_180:
mSensorX = -event.values[0];
mSensorY = -event.values[1];
break;
case Surface.ROTATION_270:
mSensorX = event.values[1];
mSensorY = -event.values[0];
}
}
}
Solution 2
Here is another example with gravitation, this will return the vector of gravity. Note that you can change the sensor type and the speed of sampling, more details here
SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
double total = Math.sqrt(x * x + y * y + z * z);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, sensor, SensorManager.SENSOR_DELAY_FASTEST);
Comments
-
Slicekick over 4 years
I am looking for some source code or example code detailing how to display the values of the accelerometer in Android. Preferably, the values would show acceleration in the x direction, y direction, and z direction.
I am an Android noob so any help is much appreciated.
-
Slicekick over 12 yearsYes, but I don't know how to use that information to display basic accelerometer values.
-
Kurtis Nusbaum over 12 yearsUpdated my answer. Check it out.
-
Slicekick over 12 yearsI see, but in the commented section, what exactly do I need to put in to read the values?
-
Kurtis Nusbaum over 12 yearsI lifted some code directly from the android accelerometer example and put it in my answer.
-
KK_07k11A0585 over 6 years@KurtisNusbaum From where you get the mDisplay.getRotation() ??
-
Kurtis Nusbaum over 6 years@KK_07k11A0585 please checkout the example I linked to
-
James Poulose about 3 yearsFor those who are looking for
getRotation
, use thisDisplay display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();