WindowManager.addView ontop(overlay) of soft key OR nav bar

家住魔仙堡 提交于 2019-12-23 05:29:24

问题


I saw an app that overlays whole screen including nav bar (or soft-key that has back, home, etc.) today. it's CF.Lumen(requires android 4.4+) by chainfire.

I just remembered this is NOT POSSIBLE on general approach and many SO answers told me. So I looked down smali codes from Lumens apk(sorry chainfire), found 0x7d6 as type specifier, which is TYPE_SYSTEM_OVERLAY. In general, this makes a view overlays on top of lock screen. It looks good however it won't overlays nav bar area. even on lock screen. I did replacing MATCH_PARENT to 9999 but it still won't overlays nav bar area.

I looked down the source code of Android, found interesting that has more types undocumented.

FIRST_SYSTEM_WINDOW = 2000;

TYPE_NAVIGATION_BAR = FIRST_SYSTEM_WINDOW+19;

TYPE_BOOT_PROGRESS = FIRST_SYSTEM_WINDOW+21;

I applied these to my app but got crashe says permission denied. It requires INTERNAL_SYSTEM_WINDOW OR something more undocumented than SYSTEM_ALERT_WINDOW. those permissions are granted for system apps only.

here's my code to add a view fills whole screen except nav bar area.

What should I do to acomplish it?(make overlay including nav bar area)

final WindowManager.LayoutParams paramsRL = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
            PixelFormat.TRANSLUCENT);
windowManager.addView(view_floating, paramsRL);

回答1:


Here's a small example which works:

    FrameLayout frameLayout = new FrameLayout(context);
    frameLayout.setBackgroundColor(Color.BLACK);
    frameLayout.setAlpha(0.5f);

    windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
                    | WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            PixelFormat.TRANSLUCENT);

    //make sure height includes the nav bar size (get the dimension of whole screen)
    params.height = screenHeight;
    params.width = screenWidth;
    windowManager.addView(frameLayout, params);

    //add your view to this frameLayout
    frameLayout.addView(....);

Three key things here are:

  1. TYPE_SYSTEM_OVERLAY (or any similar types) which can show content over the whole screen.

  2. FLAG_LAYOUT_NO_LIMITS which allows us to go over the normal size allowed.

  3. Setting the extra height required to be covered behind soft keys. The main problem was when we set the parameters to match_parent, it sets to the height of screen minus the nav bar I suppose. Setting the extra height solves the issue.



来源:https://stackoverflow.com/questions/28720679/windowmanager-addview-ontopoverlay-of-soft-key-or-nav-bar

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