Android 開發 — 顯示傳感器的值

  • 不同傳感器的 listener 只能單獨寫出,嘗試用一個 listener 監聽所有傳感器的變化失敗。
  • 某個 listener 的監聽速度設爲 SENSOR_DELAY_FASTEST ,其他的也跟着變快。
  • 溫度傳感器變化很慢,有時候沒有讀數。

 

package com.ldq.sensor.detail;

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 ExSensorDetail 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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章