Android的大部分手機中都有傳感器,傳感器類型有方向、加速度(重力)、光線、磁場、距離(臨近性)、溫度等。
方向傳感器: Sensor.TYPE_ORIENTATION
加速度(重力)傳感器: Sensor.TYPE_ACCELEROMETER
光線傳感器: Sensor.TYPE_LIGHT
磁場傳感器: Sensor.TYPE_MAGNETIC_FIELD
距離(臨近性)傳感器: Sensor.TYPE_PROXIMITY
溫度傳感器: Sensor.TYPE_TEMPERATURE
//獲取某種類型的感應器
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//注冊監聽,獲取傳感器變化值
sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
上面第三個參數為采樣率:最快、游戲、普通、用戶界面。當應用程序請求特定的采樣率時,其實只是對傳感器子系統的一個建議,不保證特定的采樣率可用。
最快: SensorManager.SENSOR_DELAY_FASTEST
最低延遲,一般不是特別敏感的處理不推薦使用,該種模式可能造成手機電力大量消耗,由於傳遞的為原始數據,算法不處理好將會影響游戲邏輯和UI的性能。
游戲: SensorManager.SENSOR_DELAY_GAME
游戲延遲,一般絕大多數的實時性較高的游戲都使用該級別。
普通: SensorManager.SENSOR_DELAY_NORMAL
標准延遲,對於一般的益智類或EASY級別的游戲可以使用,但過低的采樣率可能對一些賽車類游戲有跳幀現象。
用戶界面: SensorManager.SENSOR_DELAY_UI
一般對於屏幕方向自動旋轉使用,相對節省電能和邏輯處理,一般游戲開發中我們不使用。
使用傳感器做應用的難點在於獲取數據后如何處理,獲得相應需要的效果,這里涉及很多數學物理的知識……
下面使用一個代碼實例演示如何獲取方向和磁場強度的數據:
package com.magnetic; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { private TextView magneticView; private TextView orientationView; private TextView totalMageticView; private SensorManager sensorManager; private MySensorEventListener sensorEventListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sensorEventListener = new MySensorEventListener(); magneticView = (TextView) this.findViewById(R.id.magneticView); orientationView = (TextView) this.findViewById(R.id.orientationView); totalMageticView=(TextView) this.findViewById(R.id.totalMageticView); //獲取感應器管理器 sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); } @Override protected void onResume() { //獲取方向傳感器 @SuppressWarnings("deprecation") Sensor orientationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); sensorManager.registerListener(sensorEventListener, orientationSensor, SensorManager.SENSOR_DELAY_NORMAL); //獲取加速度傳感器 Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL); super.onResume(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private final class MySensorEventListener implements SensorEventListener { //可以得到傳感器實時測量出來的變化值 @Override public void onSensorChanged(SensorEvent event) { //得到方向的值 if(event.sensor.getType()==Sensor.TYPE_ORIENTATION) { float x = event.values[SensorManager.DATA_X]; float y = event.values[SensorManager.DATA_Y]; float z = event.values[SensorManager.DATA_Z]; orientationView.setText("方向: " + x + ", " + y + ", " + z); } //得到加速度的值 else if(event.sensor.getType()==Sensor.TYPE_MAGNETIC_FIELD) { float x = event.values[SensorManager.DATA_X]; float y = event.values[SensorManager.DATA_Y]; float z = event.values[SensorManager.DATA_Z]; magneticView.setText("合磁場強度X、Y、Z分量: " + x + ", " + y + ", " + z); //計算和磁場強度 float total=(float)(Math.sqrt(Math.pow(x,2)+Math.pow(y,2)+Math.pow(z, 2))); totalMageticView.setText("合磁場強度: " + total); } } //重寫變化 @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } } //暫停傳感器的捕獲 @Override protected void onPause() { sensorManager.unregisterListener(sensorEventListener); super.onPause(); } }
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow> <TextView android:id="@+id/orientationView" android:text="方向傳感器:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> </TableRow> <TableRow> <TextView android:id="@+id/magneticView" android:text="磁傳感器:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableRow> <TableRow> <TextView android:id="@+id/totalMageticView" android:text="合磁強度:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableRow> </TableLayout>