Hello :) I am trying to detect when the user press a long click on the button and when he releases the long click so I am using this answer: https://stackoverflow.com/a/10746549/3953319 I don't know why but the LongClickListener is called twice to me, here is my code:
button1.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View pView) {
TimeCounter = 0;
final Random rand = new Random();
time = rand.nextInt(7) + 1;
SecTime = time * 1000;
CountDownTimer2 = new CountDownTimer(SecTime, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
mPlayer = MediaPlayer.create(MainActivity.this,
R.raw.windows_8_notify);
mPlayer.start();
t = new Thread() {
@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1);
runOnUiThread(new Runnable() {
@Override
public void run() {
TimeCounter++;
textview.setText("Your reaction time:"
+ TimeCounter + "MS");
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
}
}.start();
isSpeakButtonLongPressed = true;
return true;
}
});
button1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View pView, MotionEvent pEvent) {
pView.onTouchEvent(pEvent);
if (pEvent.getAction() == MotionEvent.ACTION_UP) {
if (isSpeakButtonLongPressed) {
if (mPlayer == null) {
textview.setText("You have released the button before the sound");
CountDownTimer2.cancel();
} else {
t.interrupt();
t = null;
OldScore = sharedpreferences.getInt("HighScore", 0);
if (TimeCounter < OldScore) {
Editor editor = sharedpreferences.edit();
editor.putInt("HighScore", TimeCounter);
editor.commit();
textview1.setText("Your new High Score:"
+ TimeCounter + "MS");
}
textview.setText("Your reaction time:"
+ TimeCounter + "MS");
mPlayer.pause();
mPlayer.reset();
mPlayer = null;
}
isSpeakButtonLongPressed = false;
}
}
return false;
}
});
I ran the application and when I long click the button I hear the "windows_8_notify" twice, why is that? is there any better way to do it?