Hi,
I want to show you how you can get in real time x,y,z values of the Accelerometer sensor so that you can make any calculations for any reason you want.
This data’s will be displayed on the screen in textViews but you can use them however you want
First i will show you main.xml code:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/hello"/> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Accelerometer"/> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="X Value" android:id="@+id/xvalue"/> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Y Value" android:id="@+id/yvalue"/> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Z Value" android:id="@+id/zvalue"/> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Orientation"/> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="X Value" android:id="@+id/xvalues"/> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Y Value" android:id="@+id/yvalues"/> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Z Value" android:id="@+id/zvalues"/> <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </LinearLayout>
And this is the Main.java class:
package com.neuralnets; import android.app.Activity; import android.graphics.Color; import android.hardware.SensorListener; import android.hardware.SensorManager; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class Main extends Activity implements SensorListener { SensorManager sm = null; TextView xacc= null; TextView yacc = null; TextView zacc = null; TextView xorient = null; TextView yorient = null; TextView zorient = null; TextView text = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sm = (SensorManager) getSystemService(SENSOR_SERVICE); setContentView(R.layout.main); xacc = (TextView) findViewById(R.id.xvalue); yacc = (TextView) findViewById(R.id.yvalue); zacc = (TextView) findViewById(R.id.zvalue); xorient = (TextView) findViewById(R.id.xvalues); yorient = (TextView) findViewById(R.id.yvalues); zorient = (TextView) findViewById(R.id.zvalues); } public void onSensorChanged(int sensor, float[] values) { synchronized (this) { if (sensor == SensorManager.SENSOR_ORIENTATION) { xorient.setText("Orientation X: " + values[0]); yorient.setText("Orientation Y: " + values[1]); zorient.setText("Orientation Z: " + values[2]); } if (sensor == SensorManager.SENSOR_ACCELEROMETER) { xacc.setText("Accel X: " + values[0]); yacc.setText("Accel Y: " + values[1]); zacc.setText("Accel Z: " + values[2]); } } } public void onAccuracyChanged(int sensor, int accuracy) { Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy); } @Override protected void onResume() { super.onResume(); sm.registerListener(this, SensorManager.SENSOR_ORIENTATION | SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onStop() { sm.unregisterListener(this); super.onStop(); } }
If you run this code you will understand very well how actually works accelerometer sensor.