Accelerometer Values is a simple application that displays the accelerometer values: lateral, longitudinal, and vertical. The 3 TextViews that display the values are constantly updated to reflect the physical device's current movement. To obtain the accelerometer values, a SensorEventListener is registered to listen on changes in the accelerometer's values.
Let's run through the project and familiarize ourselves with the code. Click here to download the source files.
Open the AccelMain.java file located in the src/ directory in edu.calpoly.Samplejwong18 package
public void onCreate(Bundle savedInstanceState) is called when an Activity is first created and initializes the Activity
The Sensor and SensorManager are initialized here.
The SensorEventListener is also registered here. This is required to get updated on the changing accelerometer values.
Calls initLayout()
private void initLayout() initializes the TextViews and the layout
LinerLayout is initialized and set to a vertical orientation.
Text views are initialized and added to the layout. (size of text is set to 20 for easy viewing)
private final SensorEventListenermySensorEventListener = new SensorEventListener() {...}
public void onSensorChanged(SensorEvent sensorEvent)
When a change is detected on the accelerometer sensor, updateValues(...) is called with the accelerometer values passed in.
value[0] is the lateral value
value[1] is the longitudinal value
value[2] is the vertical value
public void onAccuracyChanged(...) is not used.
protected void onResume() will now register the SensorEventListener.
protected void onPause() and protected void onStop() will now unregister the SensorEventListener because this app no longer needs to receive updates.
private void updateValues(float lateral, float longitudinal, float vertical) will take in acclerometer values detected by the listener and update the TextViews to display the new values.
The finished app will display something like the image below.