我想听一下震动,然后完全停止加速度计并进行另一项活动。可悲的是,我没有找到任何方法来做到这一点。即使我计算一个变量并用一个简单的“如果”检查,每次检测到震动时它总是再次加载新活动。
请帮助我理解我的缺乏。
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
mSensorManager.unregisterListener(mSensorListener);
super.onPause();
}
@Override
protected void onStop() {
mSensorManager.unregisterListener(mSensorListener);
super.onStop();
}
private SensorManager mSensorManager;
private final SensorEventListener mSensorListener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
float[] value = event.values;
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float x = value[0];
float y = value[1];
float z = value[2];
//in G Umrechnung
float gX = x / gravityEarth;
float gY = y / gravityEarth;
float gZ = z / gravityEarth;
//G-Force will be 1 when there is no movement. (gravity)
gForce = FloatMath.sqrt(gX * gX + gY * gY + gZ * gZ);
double gForce2 = Math.round(gForce * 100) / 100.0;
String g = String.format("" + gForce2 + " G");
TextViewG.setTextColor(0xffff0000);
TextViewG.setTextSize(35);
TextViewG.setText(g);
}
Intent nextScreen = new Intent(getApplicationContext(), Send.class);
if (gForce > shakeThresholdInGForce ){
final long now = System.currentTimeMillis();
// ignore shake events too close to each other (500ms)
if (mShakeTimestamp + SHAKE_SLOP_TIME_MS > now ) {
mShakeTimestamp = now;
mSensorManager.unregisterListener(mSensorListener);
onStop();
startActivity(nextScreen);
}
}