|
|
Android 开发包标准有8个传感器:
- Sensor.TYPE_ACCELEROMETER
- Sensor.TYPE_GYROSCOPE
- Sensor.TYPE_LIGHT
- Sensor.TYPE_MAGNETIC_FIELD
- 磁力计 (X, Y, Z) microteslas
- Sensor.TYPE_ORIENTATION
- Sensor.TYPE_PRESSURE
- 压力传感器 (single) kilopascals 测量加在手机设备上的压力
- Sensor.TYPE_PROXIMITY
- 距离传感器 (single) meters 典型应用为接听电话时,根据光照,声音估计距离
- Sensor.TYPE_TEMPERATURE
- 温度传感器 (single) degrees Celsius 电池温度,或是具体传感器温度,看具体实现
手机型号不同,硬件实现有所区别。
读取传感器代码如下:
package com.ldq.sensor;import java.util.List;import android.app.Activity;import android.hardware.Sensor;import android.hardware.SensorManager;import android.os.Bundle;import android.widget.LinearLayout;import android.widget.TextView;public class ExSensor extends Activity {private LinearLayout layout;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);layout = (LinearLayout) findViewById(R.id.LinearLayout01);SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);List<Sensor> list = sm.getSensorList(Sensor.TYPE_ALL);TextView text = new TextView(this);text.setText("传感器数量:" + list.size());layout.addView(text);TextView[] name = new TextView[list.size()];for (int i = 0; i < list.size(); i++) {name = new TextView(this);name.setText((i + 1) + " : " + list.get(i).getName());layout.addView(name);}}} |
|