androidwindow-managersandroid-windowmanageraccessibilityservice

Is possible remove an overlay with WindowManager when press back or home button in android?


I'm trying to find some answer to my question in the site but I don't found anything, and I'm not sure if is possible remove or hide the overlay with windowManager when press back or home button.

This is that I have now. I put an overlay using accessibility service that cover all the screen when the user go to setting screen of my app. WindowManager show the overlay, but when I try to press back button or home button it doesn't work. Seems like is blocked. Only disappear the overlay when the process of the app is stopped.

Notice that I don't using a activity to show the overlay. I'm using the accessitibilyService to do this task that extends from AccessibilityService.

That I want to do is where the user press back or home button, remove the overlay.

This is my code to show the overlay:

private String checkOverlay = "hide"

 WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                    0,
                    PixelFormat.TRANSLUCENT);
            WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

            overlayRunning.setBackgroundColor(Color.parseColor("#1C1C1C"));
            wm.addView(overlayRunning, params);
checkOverlay = "show";

I've added this method the class:

 @Override
    public boolean onKeyEvent(KeyEvent event) {
        int keyCode = event.getKeyCode();

        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                Log.e(TAG, "Back button??");
                    Log.e(TAG, "Back button??");
                    overlayRunning.setVisibility(View.INVISIBLE);

            case KeyEvent.KEYCODE_HOME:
                Log.e(TAG, "Home button??");

                if(checkOverlay.equals("show")) {
                    WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
                    wm.removeViewImmediate(overlayRunning);
                    Intent startMain = new Intent(Intent.ACTION_MAIN);
                    startMain.addCategory(Intent.CATEGORY_HOME);
                    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(startMain);
                    checkOverlay = "hide";
                }
                return false;
        }
        return super.onKeyEvent(event);
    }

Solution

  • Simply you need to set the windowmanager type to APPLICATION, this way the windowmanager will attach views while the application is opened only, and no need to overlay home or back buttons, check docs here

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.MATCH_PARENT,
                        WindowManager.LayoutParams.MATCH_PARENT,
                        WindowManager.LayoutParams.TYPE_APPLICATION,
                        0,
                        PixelFormat.TRANSLUCENT);