来源: [转载]Android开发–传感器介绍 – Android移动开发技术文章_手机开发 – 红黑联盟
获取手机中的传感器的步骤为:
获取SensorManager对象
执行SensorManager对象的getDefaultSensor()方法获取Sensor对象,这里getDefaultSensor()方法要传递一个参数,指定具体的传感器类型
获取Sensor对象中的各种属性
下面给出获取手机内传感器的实现代码:
Log.d(“BruceZhang”, “以下是你的手机里的传感器:”);
List<Sensor> sensors=sensorManager.getSensorList(Sensor.TYPE_ALL);
Log.d(“BruceZhang”, “—>”);
for(Sensor sensor: sensors){
Log.d(“BruceZhang”, sensor.getName());
}
//通过使用getDefaultSensor方法获得一个特定的传感器
/ Sensor sensor=sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
在知道怎么获取传感器之后,我们需要使用程序获取传感器的值,下面给出实例代码:
SensorManager sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor sensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(new SensorEventListener() {
/*
* SensorEvent event
* 一旦传感器感知外界条件发生变化,就会把相应的数据传递给这个对象
* 一般会把这个方法放在onCreate里,不用手动的去获取
*/
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float auc=event.accuracy;
//这里的第一个元素代表的是光线的强度
float lux=event.values[0];
Log.d(“BruceZhang”, auc+””);
Log.d(“BruceZhang”, lux+””);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}, sensor, SensorManager.SENSOR_DELAY_NORMAL);
//SensorManager.SENSOR_DELAY_NORMAL
//这个参数就是设置传感器的采样率
}
传感器有一系列的属性:
1、传感器的采样率
a) SENSOR_DELAY_NOMAL (200000微秒)
b) SENSOR_DELAY_UI (60000微秒)
c) SENSOR_DELAY_GAME (20000微秒)
d) SENSOR_DELAY_FASTEST (0微秒)
2、传感器的属性
a) 传感器的名称
b) 传感器制造商
c) 传感器功率
d) 传感器的resulotion
获取这些属性的方法也很简单,代码如下:
SensorManager sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor sensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
//传感器的名字
String sensorName=event.sensor.getName();
//传感器制造商的名字
String sensorVendor=event.sensor.getVendor();
//传感器的分辨率,传感器所能识别的最小的精度是多少
float sensorRevolution=event.sensor.getResolution();
//传感器的功率
float sensorPower=event.sensor.getPower();
//传感器取得数据的时间是多少
double timeStamp=event.timestamp;
//打印数据
Log.d(“BruceZhang”, sensorName+””);
Log.d(“BruceZhang”, sensorVendor+””);
Log.d(“BruceZhang”, sensorRevolution+””);
Log.d(“BruceZhang”, sensorPower+””);
Log.d(“BruceZhang”, timeStamp+””);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
下面实现的这个实例实现了上述内容的综合:
实例的完整代码如下:
/*
* 传感器会通过传感器事件把值传给用户
* 所以,我们的应用程序是被动的,等待传感器把数据传递给SensorEvent对象
*/
public class MainActivity extends Activity {
private Button button;
private Button button2;
private Button button3;
private SensorManager sensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//得到SensorManager对象
sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
button=(Button)findViewById(R.id.sensorButton);
button2=(Button)findViewById(R.id.lightButton);
button3=(Button)findViewById(R.id.lightButton2);
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SensorManager sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor sensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
//传感器的名字
String sensorName=event.sensor.getName();
//传感器制造商的名字
String sensorVendor=event.sensor.getVendor();
//传感器的分辨率,传感器所能识别的最小的精度是多少
float sensorRevolution=event.sensor.getResolution();
//传感器的功率
float sensorPower=event.sensor.getPower();
//传感器取得数据的时间是多少
double timeStamp=event.timestamp;
//打印数据
Log.d(“BruceZhang”, sensorName+””);
Log.d(“BruceZhang”, sensorVendor+””);
Log.d(“BruceZhang”, sensorRevolution+””);
Log.d(“BruceZhang”, sensorPower+””);
Log.d(“BruceZhang”, timeStamp+””);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SensorManager sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor sensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorManager.registerListener(new SensorEventListener() {
/*
* SensorEvent event
* 一旦传感器感知外界条件发生变化,就会把相应的数据传递给这个对象
* 一般会把这个方法放在onCreate里,不用手动的去获取
*/
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float auc=event.accuracy;
//这里的第一个元素代表的是光线的强度
float lux=event.values[0];
Log.d(“BruceZhang”, auc+””);
Log.d(“BruceZhang”, lux+””);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}, sensor, SensorManager.SENSOR_DELAY_NORMAL);
//SensorManager.SENSOR_DELAY_NORMAL
//这个参数就是设置传感器的采样率
}
});
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d(“BruceZhang”, “以下是你的手机里的传感器:”);
List<Sensor> sensors=sensorManager.getSensorList(Sensor.TYPE_ALL);
Log.d(“BruceZhang”, “—>”);
for(Sensor sensor: sensors){
Log.d(“BruceZhang”, sensor.getName());
}
//通过使用getDefaultSensor方法获得一个特定的传感器
// Sensor sensor=sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}