简单的学习了一下传感器的使用,一个简单的重力传感器,在这里只是对重力传感器进行了一个简单的了解!
下面将代码放到下面,供需要学习的的参考一下,而且这里没有实现什么具体的功能!
package com.SensorTest;
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 android.widget.Toast;
public class SensorTest extends Activity implements SensorEventListener {
boolean isRegSensor = false;
private SensorManager sensorManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
List<Sensor> sensors = sensorManager
.getSensorList(Sensor.TYPE_ORIENTATION); // 方向传感器
if (sensors.size() > 0) {
Sensor sensor = sensors.get(0);
isRegSensor = sensorManager.registerListener(this, sensor,
SensorManager.SENSOR_DELAY_FASTEST);//
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
// 处理精度改变
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
if (isRegSensor) {
sensorManager.unregisterListener(this);
isRegSensor = false;
}
super.onDestroy();
}
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
// 处理方向改变等
Toast.makeText(this, "X值为:" + event.values[SensorManager.AXIS_MINUS_X],
Toast.LENGTH_LONG).show();
}
}
转自:http://www.cnblogs.com/fly_binbin/archive/2010/12/22/1914280.html