Service with Overlay - catch back button press

前端 未结 4 1348
生来不讨喜
生来不讨喜 2021-02-08 04:47

How can i do that?

Current solution

I launch a transparent activity, that catches the back press, forwards it to my service and closes itself af

4条回答
  •  我在风中等你
    2021-02-08 05:07

    I think I found out how it works... DON'T USE WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE in your overlays views + overwrite the dispatchKeyEvent of your view:

     @Override
    public boolean dispatchKeyEvent(KeyEvent event)
    {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
        {
            // handle back press
            // if (event.getAction() == KeyEvent.ACTION_DOWN)
            return true;
        }
        return super.dispatchKeyEvent(event);
    }
    

提交回复
热议问题