I have to trigger back in my application through simplest user interaction with phone, for example pressing power button 3 times. Is there any way that I could listen to power button events in service ?
Asked
Active
Viewed 1,415 times
1
-
possible duplicate of [how to start the app on power button press](http://stackoverflow.com/questions/15292376/how-to-start-the-app-on-power-button-press) – Himanshu Agarwal Oct 10 '14 at 08:58
1 Answers
0
You need to track the power key event by following code
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(KeyEvent.KEYCODE_POWER == event.getKeyCode()){
if(time interval is less)//Consider as double click
//code to do on power press
else //Consider as single click
//nothing to do
}
return super.onKeyDown(keyCode, event);
}

Himanshu Agarwal
- 4,623
- 5
- 35
- 49
-
Well, this would have worked if it have to be done in activity, in my case, I have to catch it in background service. Thanks anyways – Sabih Ahmed Oct 10 '14 at 08:53