0

我想编写一个 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();
}
    }
4

1 回答 1

1

只需更新您的 while 条件

while (!isOver) {
}
于 2013-09-09T12:47:06.213 回答