Android中支持的几种传感器:
Sensor.TYPE_ACCELEROMETER:加速度传感器。
Sensor.TYPE_GYROSCOPE:陀螺仪传感器。
Sensor.TYPE_LIGHT:亮度传感器。
Sensor.TYPE_MAGNETIC_FIELD:地磁传感器。
Sensor.TYPE_ORIENTATION:方向传感器。
Sensor.TYPE_PRESSURE:压力传感器。
Sensor.TYPE_PROXIMITY:近程传感器。
Sensor.TYPE_TEMPERATURE:温度传感器。
使用传感器最关键的一些知识是:SensorManager是所有传感器的一个综合管理类,包括了传感器的种类、采样率、精准度等。我们可以通过getSystemService方法来取得一个SensorManager对象。使用传感器时,需要通过registerListener函数注册传感器,使用完后需要通过unregisterListener取消注册。
百闻不如一见,还是直接讲代码:
新建一个Sensors的工程,创建一个Sensors.java,内容如下:
package me.sigma.sensors;
import android.app.Activity;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class Sensors extends Activity {
TextView myTextView1;//t
//gen
TextView myTextView2;//x
TextView myTextView3;//y
TextView myTextView4;//z
//acc
TextView myTextView5;//x
TextView myTextView6;//y
TextView myTextView7;//z
//ori
TextView myTextView8;//x
TextView myTextView9;//y
TextView myTextView10;//z
//Light
TextView myTextView11;//z
SensorManager mySensorManager;//
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView1 = (TextView) findViewById(R.id.myTextView1);
myTextView2 = (TextView) findViewById(R.id.myTextView2);
myTextView3 = (TextView) findViewById(R.id.myTextView3);
myTextView4 = (TextView) findViewById(R.id.myTextView4);
myTextView5 = (TextView) findViewById(R.id.myTextView5);
myTextView6 = (TextView) findViewById(R.id.myTextView6);
myTextView7 = (TextView) findViewById(R.id.myTextView7);
myTextView8 = (TextView) findViewById(R.id.myTextView8);
myTextView9 = (TextView) findViewById(R.id.myTextView9);
myTextView10 = (TextView) findViewById(R.id.myTextView10);
myTextView11 = (TextView) findViewById(R.id.myTextView11);
mySensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
private SensorListener mySensorListener = new SensorListener(){
@Override
public void onAccuracyChanged(int sensor, int accuracy) {}
@Override
public void onSensorChanged(int sensor, float[] values) {
if(sensor == SensorManager.SENSOR_TEMPERATURE){
myTextView1.setText("Current Temprature:"+values[0]);
}else if(sensor == SensorManager.SENSOR_MAGNETIC_FIELD){
myTextView2.setText("Current Magnetic x:"+values[0]);
myTextView3.setText("Current Magnetic y:"+values[1]);
myTextView4.setText("Current Magnetic z:"+values[2]);
}else if(sensor == SensorManager.SENSOR_ACCELEROMETER){
myTextView5.setText("Current Accelero x:"+values[0]);
myTextView6.setText("Current Accelero y:"+values[1]);
myTextView7.setText("Current Accelero z:"+values[2]);
}else if(sensor == SensorManager.SENSOR_ORIENTATION){
myTextView8.setText("Current Oraenttation x:"+values[0]);
myTextView9.setText("Current Oraenttation y:"+values[1]);
myTextView10.setText("Current Oraenttation z:"+values[2]);
}else if(sensor == SensorManager.SENSOR_LIGHT){
myTextView11.setText("Current Oraenttation x:"+values[0]);
}
}
};
@Override
protected void onResume() {
mySensorManager.registerListener(
mySensorListener,
SensorManager.SENSOR_TEMPERATURE |
SensorManager.SENSOR_MAGNETIC_FIELD |
SensorManager.SENSOR_ACCELEROMETER |
SensorManager.SENSOR_LIGHT |
SensorManager.SENSOR_ORIENTATION,
SensorManager.SENSOR_DELAY_UI
);
super.onResume();
}
@Override
protected void onPause() {
mySensorManager.unregisterListener(mySensorListener);
super.onPause();
}
}
更改res/layout/下面的main.xml,为如下内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/title"
android:gravity="center_horizontal"
android:textSize="20px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title"/>
<TextView
android:id="@+id/myTextView1"
android:textSize="18px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myTextView1"/>
<TextView
android:id="@+id/myTextView2"
android:textSize="18px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myTextView2"/>
<TextView
android:id="@+id/myTextView3"
android:textSize="18px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myTextView3"/>
<TextView
android:id="@+id/myTextView4"
android:textSize="18px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myTextView4"/>
<TextView
android:id="@+id/myTextView5"
android:textSize="18px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myTextView5"/>
<TextView
android:id="@+id/myTextView6"
android:textSize="18px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myTextView6"/>
<TextView
android:id="@+id/myTextView7"
android:textSize="18px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myTextView7"/>
<TextView
android:id="@+id/myTextView8"
android:textSize="18px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myTextView8"/>
<TextView
android:id="@+id/myTextView9"
android:textSize="18px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myTextView9"/>
<TextView
android:id="@+id/myTextView10"
android:textSize="18px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myTextView10"/>
<TextView
android:id="@+id/myTextView11"
android:textSize="18px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myTextView11"/>
</LinearLayout>
更改res/values/strings.xml为如下内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">templator!</string>
<string name="app_name">templator</string>
<string name="title">Sigma Sensors</string>
<string name="myTextView1">Current Temprature:</string>
<string name="myTextView2">Current Magnetic x:</string>
<string name="myTextView3">Current Magnetic y:</string>
<string name="myTextView4">Current Magnetic z:</string>
<string name="myTextView5">Current Accelero x:</string>
<string name="myTextView6">Current Accelero y:</string>
<string name="myTextView7">Current Accelero z:</string>
<string name="myTextView8">Current Oraenttation x:</string>
<string name="myTextView9">Current Oraenttation y:</string>
<string name="myTextView10">Current Oraenttation z:</string>
<string name="myTextView11">Current Light:</string>
</resources>