Android 开发 — 展示传感器的值

Android 开发 — 显示传感器的值不同传感器的 listener 只能单独写出,尝试用一个 listener 监听所有传感器

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() {@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {// TODO Auto-generated method stub}@Overridepublic void onSensorChanged(SensorEvent event) {// TODO Auto-generated method stubLog.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() {@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {// TODO Auto-generated method stub}@Overridepublic void onSensorChanged(SensorEvent event) {// TODO Auto-generated method stubtext4.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() {@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {// TODO Auto-generated method stub}@Overridepublic void onSensorChanged(SensorEvent event) {// TODO Auto-generated method stubtext7.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() {@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {// TODO Auto-generated method stub}@Overridepublic void onSensorChanged(SensorEvent event) {// TODO Auto-generated method stubtext10.setText("TEMPERATURE: " + event.values[0]);}};/** Called when the activity is first created. */@Overridepublic 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>

?