Android Sensor 感應器的開發

 
主要還是focus在 SensorEventListener上面, ( 我是後來才知道SensorListener已經不用了!!....T_T )
廢話不多說,  直接來看程式碼!!
public class SensorDetail extends Activity {
	private TextView text1;
	private TextView text2;
	private TextView text3;
	private TextView text4;
	private TextView text5;
	private TextView text6;
	private TextView text7;
	private TextView text8;
	private TextView text9;
	private TextView text10;

	private SensorEventListener acc_listener = new 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
			Log.i("------", "" + "TYPE_ACCELEROMETER");
			text1.setText("ACCELEROMETER_X: " + event.values[0]);
			text2.setText("ACCELEROMETER_Y: " + event.values[1]);
			text3.setText("ACCELEROMETER_Z: " + event.values[2]);
		}
	};

	private SensorEventListener mag_listener = new 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
			text4.setText("MAGNETIC_FIELD_X: " + event.values[0]);
			text5.setText("MAGNETIC_FIELD_Y: " + event.values[1]);
			text6.setText("MAGNETIC_FIELD_Z: " + event.values[2]);
		}
	};

	private SensorEventListener ori_listener = new 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
			text7.setText("ORIENTATION_X: " + event.values[0]);
			text8.setText("ORIENTATION_Y: " + event.values[1]);
			text9.setText("ORIENTATION_Z: " + event.values[2]);
		}
	};

	private SensorEventListener tem_listener = new 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
			text10.setText("TEMPERATURE: " + event.values[0]);
		}
	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		text1 = (TextView) findViewById(R.id.TextView01);
		text2 = (TextView) findViewById(R.id.TextView02);
		text3 = (TextView) findViewById(R.id.TextView03);
		text4 = (TextView) findViewById(R.id.TextView04);
		text5 = (TextView) findViewById(R.id.TextView05);
		text6 = (TextView) findViewById(R.id.TextView06);
		text7 = (TextView) findViewById(R.id.TextView07);
		text8 = (TextView) findViewById(R.id.TextView08);
		text9 = (TextView) findViewById(R.id.TextView09);
		text10 = (TextView) findViewById(R.id.TextView01);

		SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
		sm.registerListener(acc_listener, sm
				.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_NORMAL);
		sm.registerListener(mag_listener, sm
				.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
				SensorManager.SENSOR_DELAY_NORMAL);
		sm.registerListener(ori_listener, sm
				.getDefaultSensor(Sensor.TYPE_ORIENTATION),
				SensorManager.SENSOR_DELAY_NORMAL);
		sm.registerListener(tem_listener, sm
				.getDefaultSensor(Sensor.TYPE_TEMPERATURE),
				SensorManager.SENSOR_DELAY_NORMAL);

	}
}

 main.xml 文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/LinearLayout01"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical">
	<TextView android:text="@+id/TextView01" android:id="@+id/TextView01"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
	<TextView android:text="@+id/TextView02" android:id="@+id/TextView02"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
	<TextView android:text="@+id/TextView03" android:id="@+id/TextView03"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
	<TextView android:text="@+id/TextView04" android:id="@+id/TextView04"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_marginTop="10px"></TextView>
	<TextView android:text="@+id/TextView05" android:id="@+id/TextView05"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
	<TextView android:text="@+id/TextView06" android:id="@+id/TextView06"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
	<TextView android:text="@+id/TextView07" android:id="@+id/TextView07"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_marginTop="10px"></TextView>
	<TextView android:text="@+id/TextView08" android:id="@+id/TextView08"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
	<TextView android:text="@+id/TextView09" android:id="@+id/TextView09"
		android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
	<TextView android:text="@+id/TextView10" android:id="@+id/TextView10"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_marginTop="10px"></TextView>
</LinearLayout>
廣告

About fenjj

Perfect !!??...
本篇發表於 Uncategorized。將永久鏈結加入書籤。

One Response to Android Sensor 感應器的開發

  1. Tamara 說道:

    Hey there! I’ve been following your web site for a while now and finally got the bravery to
    go ahead and give you a shout out from Humble Tx! Just wanted to tell you keep up the great work!

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s