首页 > 代码库 > 关于传感器
关于传感器
类extends Activity implements SensorEventListener
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.sensor.getType()==Sensor.TYPE_TEMPERATURE){
Log.e("Log", "temprature==="+event.values[0]);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
传感器
private SensorManager mSensorManager;
用于判断是否注册成功
boolean mRegisteredSensor;
protected void onResume() {
super.onResume();
mSensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
List<Sensor> sensors=mSensorManager.getSensorList(Sensor.TYPE_ALL);//获得一个方向传感器
for (Sensor s : sensors) {
Log.e("Log", "sensors.name==="+s.getType());
}
if (sensors.size()>0)
{
Sensor sensor=sensors.get(0);
mRegisteredSensor=mSensorManager.registerListener(this,sensor,SensorManager.SENSOR_DELAY_FASTEST);
Log.e("Log", "booelan =="+mRegisteredSensor);
}
}
protected void onPause()
{
if (mRegisteredSensor) {
mSensorManager.unregisterListener(this);
mRegisteredSensor=false;
}
super.onPause();
}
关于传感器