How to add a floating view to Android Window manager and listen to System/Hardware back button events

那年仲夏 提交于 2019-11-28 06:02:31
qbasso

In terms of back button detection - I made it to work in a following way (everything happens in service onCreate code):

  1. Wrap your desired view into ViewGroup (LinearLayout, Relative or other)
  2. override dispatchKeyEvent like this in wrapper view:

mView = new RelativeLayout(this) {
        @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            if (event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
                // < your action >
                return true;
            }
            return super.dispatchKeyEvent(event);
        }
};
  1. add wrapper view to the window manager, and be sure that WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE is not set on wrapper layout params.

Have a look at Standout Library , which is good for handling floating windows , it seems it is also not handling back press event , contacting developer might help.

And one more way is you can try opening activity with semi transparent background/theme to get the similar effect used in floating window in Any.do and backpress event can be handled

Regarding the Back button - You should override the "onBackPressed()" inside your view and do whatever you want

@Override
public boolean onBackPressed() {
    // Remove your view from the window...
}

Anyway, I'm using an SDK called Tooleap, to display floating windows in a straight-forward way.

Do you want the HOME button to also dismiss your UI? If you do, it sounds like it is better to have an activity that opens on a transparent background, instead of an alert window. To do this use the following style as the theme for your activity

<style name="Transparent">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowFullscreen">true</item>
</style>

FOR WORKING "onBackpressed" BUTTON FOLLOW BELOW INSTRUCTION

1.Go to FlotingFolder.java file in example (Not library).

2.Find below method

@Override
public int getFlags(int id) {
    if (APP_SELECTOR_ID == id) {
        return super.getFlags(id);
    } else {
        return super.getFlags(id) | StandOutFlags.FLAG_BODY_MOVE_ENABLE
                | StandOutFlags.FLAG_WINDOW_EDGE_LIMITS_ENABLE
                | StandOutFlags.FLAG_WINDOW_FOCUSABLE_DISABLE;
    }
}
  1. Then remove - "StandOutFlags.FLAG_WINDOW_FOCUSABLE_DISABLE" from the above method

Now onBackPressed will work.

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