package com.king.android.speical;
import java.util.List;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import com.king.android.R;
/**
* 描述:传感器
* 作者:Andy.Liu
* 时间: 2012-7-16 上午07:22:19
**/
public class SensorActivity extends Activity implements SensorEventListener {
private boolean mRegisterSensor = false;
private SensorManager mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
if(sensors.size()>0){
Sensor sensor = sensors.get(0);
//注册Sensor ,接收频率。
mRegisterSensor = mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST);
}
}
@Override
protected void onPause() {
if(mRegisterSensor){
mSensorManager.unregisterListener(this);
mRegisterSensor = false;
}
super.onPause();
}
//当精确值发生改变时、
//Accuracy精确值
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
//处理精确值改变
}
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType()==Sensor.TYPE_ORIENTATION){
float x = event.values[SensorManager.DATA_X];
float y = event.values[SensorManager.DATA_Y];
float z = event.values[SensorManager.DATA_Z];
}
}
}
/**
SensorManager中的其它方法
getDefaultSensor:得到默认的传感器对象
getInclination:得到地磁传感器倾斜的弧度值
getOrientation:得到设备的旋转方向
getSensorList:得到指定传感器列表。
**/