SensorEventListener与Timer-Thread结合使用

时间:2013-09-09 12:43:49

标签: android multithreading timer sensor countdown

我想写一个Android程序,让用户设置一个倒数计时器。用户按下“开始 - 按钮”后,倒计时正在运行。倒计时正在运行时,手机应识别设备的加速度。

如果时间到了,或者设备被动摇了很多,那么应该加载一个新的活动..

在我的设备(S4)中,代码运行良好,只有倒数计时器在设备被抖动后才会关闭,因此运行方法将突然激活。在其他设备(S2)上,按下按钮后直接启动新活动。怎么会这样? 我认为我在运行方法上犯了一个错误,我不确定我是否理解正确设置线程的想法。

这是我的代码:

public class WaitingForBomb extends Activity {

float sensibility = 2.5f;
boolean isOver = false;
private SensorManager mSensorManager;
private float mAccel; // Beschleunigung
private float mAccelCurrent; // aktuelle Beschleunigung (in Verbindung mit
                                // Erdanziehung)?
private float mAccelLast; // letzte Beschleunigung (in Verbindung mit
                            // Erdanziehung)?
TextView anzeige;

@Override
protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.waiting);

    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensorManager.registerListener(mSensorListener,
            mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_NORMAL);
    mAccel = 0.00f;
    mAccelCurrent = SensorManager.GRAVITY_EARTH;
    mAccelLast = SensorManager.GRAVITY_EARTH;

    // Sleep-Timer
    Thread bombTimer = new Thread() {
        public void run() {
            while (isOver = false) {
                try {
                    // Intent-Übergabe des Counters
                    Intent mIntent = getIntent();
                    int counterValue = mIntent
                            .getIntExtra("pushCounter", 0);
                    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                    v.vibrate(counterValue);
                    sleep(counterValue);

                    isOver = true;

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    isOver = true;
                }
            }
        }
    };

    bombTimer.start();

}

// Verhindert, dass die "Back-Taste" erkannt wird und somit das Spiel
// vorzeitig beendet wird.
@Override
public void onBackPressed() {
}

private final SensorEventListener mSensorListener = new SensorEventListener() {

    public void onSensorChanged(SensorEvent se) {
        float x = se.values[0];
        float y = se.values[1];
        float z = se.values[2];
        mAccelLast = mAccelCurrent;
        mAccelCurrent = (float) Math.sqrt((double) (x * x + y * y + z * z));
        float delta = mAccelCurrent - mAccelLast;
        mAccel = mAccel * 0.9f + delta; // perform low-cut filter

        SharedPreferences getPrefs = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext());
        boolean checkSense1 = getPrefs.getBoolean("sensi1", true);
        boolean checkSense2 = getPrefs.getBoolean("sensi2", false);

        if (checkSense1 == true) {
            sensibility = 1.25f;
        }

        if (checkSense2 == true) {
            sensibility = 0.75f;
        }

        // Prüft die Beschleunigung
        if (mAccel > sensibility) {
            Intent gameOver = new Intent("android.intent.action.BOOM");
            startActivity(gameOver);
        }
        if (isOver == true) {
            Intent gameOver = new Intent("android.intent.action.BOOM");
            startActivity(gameOver);
        }
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
};

@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();
    finish();
}
    }

1 个答案:

答案 0 :(得分:1)

只需更新您的条件

while (!isOver) {
}