So we will be using a TextView to display the data.
Setup your project and open the activity_main.xml and edit the default Hello world text to:
<TextView android:id="@+id/display_reading" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
That is all we need to do to our activity_main.xml
Next we have to create a SensorManager and Sensor variable as follows
Sensor sensor; //help us manage sensor components SensorManager sm TextView displayReading;We then configure our service and select the type of service we wish to utilize in this case the sensor service, after that we then select a sensor (Accelerometer)
//setup a sensor service sm = (SensorManager)getSystemService(SENSOR_SERVICE); //select the sensor we wish to use sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sm.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL); displayReading = (TextView)findViewById(R.id.display_reading);you will notice we implemented a sensor manager listener to sm.registerListener after doing this you will get an error which can be resolved by implementing SensorEventListener. After doing this you will get another error which is resolved by implementing to methods; onSensorChanged() and onAccuracyChanged()
We are all done with setting things up now we have to get our readings, which we implement in the onSensorChanged() method:
@Override public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub displayReading.setText("X"+event.values[0]+"\nY"+event.values[1]+"\nZ"+event.values[2]); }
The Full code
package com.example.accelerometer; import android.app.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.media.MediaPlayer; import android.os.Bundle; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity implements SensorEventListener{ //this class help select a particular sensor Sensor sensor; //help us manage sensor components SensorManager sm; TextView displayReading; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //setup a sensor service sm = (SensorManager)getSystemService(SENSOR_SERVICE); //select the sensor we wish to use sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sm.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL); displayReading = (TextView)findViewById(R.id.display_reading); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onAccuracyChanged(Sensor arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub displayReading.setText("X"+event.values[0]+"\nY"+event.values[1]+"\nZ"+event.values[2]); } }
With all that done you can now test "on a real device" since the emulator can not simulate accelerometer behavior, i will get a snapshot of it, thanks.
No comments:
Post a Comment