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.widget.TextView;
public class SensorTestActivity extends Activity {
TextView tvXaxis,tvYaxis,tvZaxis;
List<Sensor> sensors;
SensorManager sensorManager;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initUi();
sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
sensors=sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
tvXaxis.setText( "sensor name : "+sensors.get(0).getName() );
tvYaxis.setText( "sensor vendor : "+sensors.get(0).getVendor() );
tvZaxis.setText( "sensor version : "+sensors.get(0).getVersion() );
}
public void initUi()
{
tvXaxis=(TextView)findViewById(R.id.textViewXaxis);
tvYaxis=(TextView)findViewById(R.id.textViewYaxis);
tvZaxis=(TextView)findViewById(R.id.textViewZaxis);
}
public class MySensorEventListener implements SensorEventListener
{
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event)
{
// TODO Auto-generated method stub
}
}
}
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>
No comments:
Post a Comment