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

前端 未结 8 1280
暗喜
暗喜 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 10:06

    Here is working sample code.

    mLinear =  new LinearLayout(getApplicationContext()) {
    
              //home or recent button
              public void onCloseSystemDialogs(String reason) {
                  //The Code Want to Perform. 
              }
    
              @Override
              public boolean dispatchKeyEvent(KeyEvent event) {
                  if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
                    || event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP
                    || event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN
                    || event.getKeyCode() == KeyEvent.KEYCODE_CAMERA ) {
    
                  //The Code Want to Perform.
                  }
              return super.dispatchKeyEvent(event);
              }
    };
    
    mLinear.setFocusable(true);
    
    View mView = inflate.inflate(R.layout.floating_panel_layout, mLinear);
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    
    //params
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                 width,
                 height,
                 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                 WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_FULLSCREEN
                        | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                  PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
    wm.addView(mView, params);
    

提交回复
热议问题