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

两盒软妹~` 提交于 2020-01-07 03:00:10

问题


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);
    }

回答1:


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);



回答2:


Removing the view is easy. I noticed you have set it to invisible, this is an acceptable solution. You can also keep a reference to it around and do the following:

windowManager.removeView(mYourView);

The problem is going to be detecting the back button press. You cannot do this reliably. You're relying on a hardware expectation, namely that the back button press sends a key event with KEYCODE_BACK, and the OS does not make this guarantee. However, on my Droid turbo the following works:

First you need a separate xml file to configure your accessibility service.

contents of service_config.xml:

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    android:canRequestFilterKeyEvents="true"
    android:accessibilityFlags="flagDefault|flagRequestFilterKeyEvents"
    ...
    />

Contents of AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>
    <application ...>
        <service ...>
            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/service_config" />
        </service>
    </application>
</manifest>


来源:https://stackoverflow.com/questions/35575537/is-possible-remove-an-overlay-with-windowmanager-when-press-back-or-home-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!