使用了方向和加速度,重力sensor。代码里面包含了简单使用和shake测试。对方向的使用了两个方法:方向sensor或者加速度+重力
不多说,上代码。
public class Testsensor extends Activity {
/** Called when the activity is first created. */
private LinearLayout layout;
private Sensor orientsensor, lightsensor, accesensor, proxsensor, msensor;
SensorManager sm;
TextView outputtext1, outputtext2, outputtext3;
float avalue[] = new float[3];
float mvalue[] = new float[3];
long lastUpdate =0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (LinearLayout) findViewById(R.id.LinearLayout01);
outputtext1 = new TextView(this);
outputtext2 = new TextView(this);
outputtext3 = new TextView(this);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
List<Sensor> list = sm.getSensorList(Sensor.TYPE_ALL);
//TextView text = new TextView(this);
// text.setText("传感器数量:" + list.size());
//layout.addView(text);
TextView[] name = new TextView[list.size()];
for (int i = 0; i < list.size(); i++) {
name[i] = new TextView(this);
name[i].setText((i + 1) + " : " + list.get(i).getName());
layout.addView(name[i]);
}
layout.addView(outputtext1);
layout.addView(outputtext2);
layout.addView(outputtext3);
orientsensor = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
// lightsensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
accesensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// proxsensor = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
msensor =sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
boolean res;
res = sm.registerListener(Orientlistener, orientsensor, SensorManager.SENSOR_DELAY_FASTEST);
if(res == false)
Toast.makeText(this, "Can not support light", Toast.LENGTH_SHORT).show();
res = sm.registerListener(Accelistener, accesensor, SensorManager.SENSOR_DELAY_FASTEST);
if(res == false)
Toast.makeText(this, "Can not support accelate", Toast.LENGTH_SHORT).show();
res = sm.registerListener(Accelistener, msensor, SensorManager.SENSOR_DELAY_FASTEST);
if(res == false)
Toast.makeText(this, "Can not support accelate", Toast.LENGTH_SHORT).show();
super.onResume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
sm.unregisterListener(Orientlistener, orientsensor);
sm.unregisterListener(Accelistener, accesensor);
sm.unregisterListener(Accelistener, msensor);
super.onPause();
}
//新建一个传感器事件监听器,重写其方法,主要是重写 onSensorChanged
SensorEventListener Orientlistener = new SensorEventListener(){
public void onSensorChanged(SensorEvent event) {
//values[0]: Azimuth, angle between the magnetic north direction and the Y axis,
//around the Z axis (0 to 359).
//0=North, 90=East, 180=South, 270=West
float orentation1 = event.values[0];
float orentation2 = event.values[1];
float orentation3 = event.values[2];
String st = String.format(" 1: Azimuth = "+orentation1 + "/nPitch = " + orentation2 + "/nRoll = " + orentation3);
outputtext1.setText(st);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
SensorEventListener Accelistener = new SensorEventListener(){
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
long curTime = System.currentTimeMillis();
if ((curTime - lastUpdate) > 100) {
long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;
float speed = Math.abs(avalue[0]+avalue[1]+avalue[2]
- event.values[0] - event.values[1] - event.values[2]) / diffTime * 10000;
if (speed > 350) {
String st = String.format("shake detected");
OutputInfo(st);
}
avalue[0] = event.values[0];
avalue[1] = event.values[1];
avalue[2] = event.values[2];
String st = String.format("Gx = "+avalue[0] + "/nGy = " + avalue[1] + "/nGz = " + avalue[2]);
outputtext3.setText(st);
}
}else
{
mvalue[0] = event.values[0];
mvalue[1] = event.values[1];
mvalue[2] = event.values[2];
}
float mR[] = new float [16];
float mI[] = new float [16];
SensorManager.getRotationMatrix(mR, mI, avalue, mvalue);
float mOrientation[] = new float [3];
SensorManager.getOrientation(mR, mOrientation);
// float incl = SensorManager.getInclination(mI);
final float rad2deg = (float)(180.0f/Math.PI);
String st = String.format("2: Azimuth = "+mOrientation[0]*rad2deg + "/nPitch = " + mOrientation[1]*rad2deg + "/nRoll = " + mOrientation[2]*rad2deg);
outputtext2.setText(st);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
void OutputInfo(String st)
{
Toast.makeText(this, st, Toast.LENGTH_SHORT).show();
}
}