只提供参考,自己还在学习中
方法有:
public class SensorActivity extends Activity implements SensorEventListener {
/** Called when the activity is first created. */
private boolean mRegisteredSensor;
private SensorManager mSensorManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRegisteredSensor = false;
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// 接收sensorManager的一个列表(Listener)
// 这里我们指定类型为type_ORIENTATION(方向感应器)
List<Sensor> sensors = mSensorManager
.getSensorList(Sensor.TYPE_ORIENTATION);
if (sensors.size() > 0) {
Sensor sensor = sensors.get(0);
mRegisteredSensor = mSensorManager.registerListener(this, sensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
}
@Override
protected void onPause() {
if (mRegisteredSensor) {
mSensorManager.unregisterListener(this);
mRegisteredSensor = false;
}
super.onPause();
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// 处理精准确改变
}
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];
}
}
}