SensorTestActivity.java
--------------------------------
package home.SensorTest;
import java.util.List;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class SensorTestActivity extends Activity implements SensorEventListener
{
TextView tvXaxis,tvYaxis,tvZaxis;
List<Sensor> sensors;
SensorManager sensorManager;
Sensor accSensor;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initUi();
sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
// add listener. The listener will be HelloAndroid (this) class
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
public void initUi()
{
tvXaxis=(TextView)findViewById(R.id.textViewXaxis);
tvYaxis=(TextView)findViewById(R.id.textViewYaxis);
tvZaxis=(TextView)findViewById(R.id.textViewZaxis);
}
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
}
public void onSensorChanged(SensorEvent event)
{
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER)
{
updateView(event.values[0], event.values[1], event.values[2]);
}
}
public void updateView( Float x, Float y, Float z )
{
tvXaxis.setText("x axis = "+x.toString() );
tvYaxis.setText("y axis ="+y.toString() );
tvZaxis.setText("z axis ="+z.toString() );
}
}
main.xml
--------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sensor Test Activity" />
<TextView
android:id="@+id/textViewXaxis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x axis"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textViewYaxis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="y axis"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textViewZaxis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="z axis"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
AndroidManifest.xml
------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="home.SensorTest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SensorTestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment