這里以重力傳感器為例說明
第一步:創建一個SensorManager對象,用來管理或者獲取傳感器
SensorManager sm = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
第二步:根據Sensor下枚舉獲得對應的傳感器對象
Sensor sr = sm.getDefaultSensor(Sensor.TYPE_GRAVITY);
第三步,為該傳感器注冊一個事件,方法有很多種,可以讓Activi繼承SensorEventListener接口,或者直接使用內部類,推薦使用內部類:
sm.registerListener(new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }, sr, SensorManager.SENSOR_DELAY_NORMAL);
其中,第三個參數,SENSOR_DELAY_NORMAL表示傳感器反應速度,這里SENSOR_DELAY_GAME,SENSOR_DELAY_FAST等。
第四步:SensorEvent對象中的values[]數組中保存了傳感器具體數值,針對不同的傳感器對象,values[]具有不同內容,長度也不一樣。對於Gravity傳感器,這里values有三個值,分別代表了其在x,y,z方向上的重力加速度,我們在xml文件中簡單設置一下布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_gravity" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.xdwang.myapplication.Gravity" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/x"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/y"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/z"/> </LinearLayout>
然后在onSensorChanged中對三個TXTVIEW設置值:
sm.registerListener(new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { txtx.setText("x方向的重力加速度為:" + event.values[0] + "g/ms2"); txty.setText("y方向的重力加速度為:" + event.values[1] + "g/ms2"); txtz.setText("z方向的重力加速度為:" + event.values[2] + "g/ms2"); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }, sr, SensorManager.SENSOR_DELAY_NORMAL);
這樣,我們就能實時得到重力傳感器的數據了,具體怎么使用這些數據,就要看你的用處了。
最后附上全部代碼:
import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class Gravity extends AppCompatActivity { TextView txtx = null; TextView txty = null; TextView txtz = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gravity); SensorManager sm = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE); Sensor sr = sm.getDefaultSensor(Sensor.TYPE_GRAVITY); txtx = (TextView) findViewById(R.id.x); txty = (TextView) findViewById(R.id.y); txtz = (TextView) findViewById(R.id.z); sm.registerListener(new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { txtx.setText("x方向的重力加速度為:" + event.values[0] + "g/ms2"); txty.setText("y方向的重力加速度為:" + event.values[1] + "g/ms2"); txtz.setText("z方向的重力加速度為:" + event.values[2] + "g/ms2"); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }, sr, SensorManager.SENSOR_DELAY_NORMAL); } }