简体   繁体   中英

Detect double press power button in android background service

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 ?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM