手機全部傳感器的都添加到栗子中了android
package com.example.senortest; 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.view.Menu; import android.view.MenuItem; import android.widget.EditText; public class MainActivity extends Activity implements SensorEventListener { // 定義Sensor管理器 private SensorManager mSensorManager; EditText etOrientation; EditText etGyro; EditText etMagnetic; EditText etGravity; EditText etLinearAcc; EditText etTemerature; EditText etLight; EditText etPressure; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 獲取界面上的EditText組件 etOrientation = (EditText) findViewById(R.id.etOrientation); etGyro = (EditText) findViewById(R.id.etGyro); etMagnetic = (EditText) findViewById(R.id.etMagnetic); etGravity = (EditText) findViewById(R.id.etGravity); etLinearAcc = (EditText) findViewById(R.id.etLinearAcc); etTemerature = (EditText) findViewById(R.id.etTemerature); etLight = (EditText) findViewById(R.id.etLight); etPressure = (EditText) findViewById(R.id.etPressure); // 獲取傳感器管理服務 mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); // ① } @Override protected void onResume() { super.onResume(); // 爲系統的方向傳感器註冊監聽器 mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME); // 爲系統的陀螺儀傳感器註冊監聽器 mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME); // 爲系統的磁場傳感器註冊監聽器 mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_GAME); // 爲系統的重力傳感器註冊監聽器 mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY), SensorManager.SENSOR_DELAY_GAME); // 爲系統的線性加速度傳感器註冊監聽器 mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION), SensorManager.SENSOR_DELAY_GAME); // 爲系統的溫度傳感器註冊監聽器 mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE), SensorManager.SENSOR_DELAY_GAME); // 爲系統的光傳感器註冊監聽器 mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_GAME); // 爲系統的壓力傳感器註冊監聽器 mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE), SensorManager.SENSOR_DELAY_GAME); } @Override protected void onStop() { // 程序退出時取消註冊傳感器監聽器 mSensorManager.unregisterListener(this); super.onStop(); } @Override protected void onPause() { // 程序暫停時取消註冊傳感器監聽器 mSensorManager.unregisterListener(this); super.onPause(); } // 如下是實現SensorEventListener接口必須實現的方法 @Override // 當傳感器精度改變時回調該方法。 public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override public void onSensorChanged(SensorEvent event) { float[] values = event.values; // 獲取觸發event的傳感器類型 int sensorType = event.sensor.getType(); StringBuilder sb = null; // 判斷是哪一個傳感器發生改變 switch (sensorType) { // 方向傳感器 case Sensor.TYPE_ORIENTATION: sb = new StringBuilder(); sb.append("繞Z軸轉過的角度:"); sb.append(values[0]); sb.append("\n繞X軸轉過的角度:"); sb.append(values[1]); sb.append("\n繞Y軸轉過的角度:"); sb.append(values[2]); etOrientation.setText(sb.toString()); break; // 陀螺儀傳感器 case Sensor.TYPE_GYROSCOPE: sb = new StringBuilder(); sb.append("繞X軸旋轉的角速度:"); sb.append(values[0]); sb.append("\n繞Y軸旋轉的角速度:"); sb.append(values[1]); sb.append("\n繞Z軸旋轉的角速度:"); sb.append(values[2]); etGyro.setText(sb.toString()); break; // 磁場傳感器 case Sensor.TYPE_MAGNETIC_FIELD: sb = new StringBuilder(); sb.append("X軸方向上的磁場強度:"); sb.append(values[0]); sb.append("\nY軸方向上的磁場強度:"); sb.append(values[1]); sb.append("\nZ軸方向上的磁場強度:"); sb.append(values[2]); etMagnetic.setText(sb.toString()); break; // 重力傳感器 case Sensor.TYPE_GRAVITY: sb = new StringBuilder(); sb.append("X軸方向上的重力:"); sb.append(values[0]); sb.append("\nY軸方向上的重力:"); sb.append(values[1]); sb.append("\nZ方向上的重力:"); sb.append(values[2]); etGravity.setText(sb.toString()); break; // 線性加速度傳感器 case Sensor.TYPE_LINEAR_ACCELERATION: sb = new StringBuilder(); sb.append("X軸方向上的線性加速度:"); sb.append(values[0]); sb.append("\nY軸方向上的線性加速度:"); sb.append(values[1]); sb.append("\nZ軸方向上的線性加速度:"); sb.append(values[2]); etLinearAcc.setText(sb.toString()); break; // 溫度傳感器 case Sensor.TYPE_AMBIENT_TEMPERATURE: sb = new StringBuilder(); sb.append("當前溫度爲:"); sb.append(values[0]); etTemerature.setText(sb.toString()); break; // 光傳感器 case Sensor.TYPE_LIGHT: sb = new StringBuilder(); sb.append("當前光的強度爲:"); sb.append(values[0]); etLight.setText(sb.toString()); break; // 壓力傳感器 case Sensor.TYPE_PRESSURE: sb = new StringBuilder(); sb.append("當前壓力爲:"); sb.append(values[0]); etPressure.setText(sb.toString()); break; } } }
佈局XMLapp
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/orientation" /> <EditText android:id="@+id/etOrientation" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/gyro" /> <EditText android:id="@+id/etGyro" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/magnetic" /> <EditText android:id="@+id/etMagnetic" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/gravity" /> <EditText android:id="@+id/etGravity" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/linearAcc" /> <EditText android:id="@+id/etLinearAcc" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/temperature" /> <EditText android:id="@+id/etTemerature" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/light" /> <EditText android:id="@+id/etLight" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/pressure" /> <EditText android:id="@+id/etPressure" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false" /> </LinearLayout> </ScrollView>
stringside
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">傳感器測試</string> <string name="orientation">方向傳感器返回的值</string> <string name="gyro">陀螺儀傳感器返回的值</string> <string name="magnetic">磁場傳感器返回的值</string> <string name="gravity">重力傳感器返回的值</string> <string name="linearAcc">線性加速度傳感器返回的值</string> <string name="temperature">溫度傳感器返回的值</string> <string name="light">光傳感器返回的值</string> <string name="pressure">壓力傳感器返回的值</string> </resources>
要在真機測試 佈局