Service with Overlay - catch back button press

前端 未结 4 1347
生来不讨喜
生来不讨喜 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:11

    If you have an overlay window attached in a WindowManager in your service, put your view the attribute setFocusableInTouchMode(true), and put a key listener. You'll receive key events, like in this example:

    view.setFocusableInTouchMode(true);
    view.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(keyCode == KeyEvent.KEYCODE_BACK) {
                stopSelf();
                return true;
            }
            return false;
        }
    });
    

    Edited, I didn't use good stackoverflow account

提交回复
热议问题