Is possible remove an overlay with WindowManager when press back or home button in android?
<p>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.</p>

<p>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.</p>

<p>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.</p>

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

<p>This is my code to show the overlay:</p>

<pre><code>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";
</code></pre>

<p>I've added this method the class:</p>

<pre><code> @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);
    }
</code></pre>