Android, How to receive home button click through broadcast receiver?

ぐ巨炮叔叔 提交于 2019-12-01 13:39:40

Why dont you use dispatchKeyEvent function. It can intercept the HOME button pressing event.

@Override
    public boolean dispatchKeyEvent(KeyEvent keyevent) {

        if (keyevent.getKeyCode() == KeyEvent.KEYCODE_HOME) {
            //Do here what you want
            return true;
        }
        else
            return super.dispatchKeyEvent(event);
   }

What you can do is like this

Create your Home button event catcher

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode==KeyEvent.KEYCODE_HOME){
       //Send your own custom broadcast message
    }
    return super.onKeyDown(keyCode, event);
}

Now you can create a broadcast receiver so that you can receive your message

You can refer here and here for how to send a broadcast messsage

Binoy Babu

If you just need to find out when ever the user goes out of application , why don't you use the onPause(..) method of the Activity?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!