Detect 'home button pressed' event in android service displaying a UI (similar to facebook chatheads)

前端 未结 8 1262
暗喜
暗喜 2020-12-13 09:35

In facebook chatheads, that are part of the facebook messenger app, I noticed the following behavior: As far as I can see, the chat head itself and the opened chat screen ar

8条回答
  •  天命终不由人
    2020-12-13 09:58

    ok complete edit of my answer,

    Override below method in your activity.

     @Override
        public void onAttachedToWindow() {
            super.onAttachedToWindow();
            this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
        }
    

    After overriding above method, now you can easily listen HOME Key press in your activity using onKeyDown() method.

      @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {     
    
            if(keyCode == KeyEvent.KEYCODE_HOME)
            {
               //The Code Want to Perform. 
            }
        });
    

    try this hope it helps

提交回复
热议问题