I'm new in Android. I'm trying to make an app that will run in background and detect three rapid presses of the power button. I've looked up a lot, but could not clear my confusion. can anyone please give me some suggestion? TIA.
Asked
Active
Viewed 1,405 times
2 Answers
1
Declare the static variable outside the onKeyDown
and increment variable inside the onKeyDown
and return if the value is equal to 3 and at the end again equal the static variable equal to 0;
static int i=0;
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
i++;
if(i==3){
//do something
//at the end again i=0;
}
}
return super.onKeyDown(keyCode, event);
}

Nambi
- 11,944
- 3
- 37
- 49
0
You could listen for every press of the power button, then in your listener you could
- measure how many presses you have just made up to this point
- measure the time between your current press and your last press
- if your time interval is right (say 200 ms) then increment your n_presses and do whatever you want after you reach 3 presses (e.g. create a super event and send it to a thread to process it)

webuster
- 2,490
- 18
- 27