Android Window Manager block system back key
Asked Answered
R

1

7

I have an floating view like Facebook chat buble, but unlike it, my overlay has a EditText and need to be focusable.

Here is my floating view code

        //Inflate the floating view layout we created
        mFloatingView = LayoutInflater.from(this).inflate(R.layout.floating_widget_layout, null);

        //Add the view to the window.
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.TRANSLUCENT);

        //Specify the view position
        params.gravity = Gravity.TOP | Gravity.START;        //Initially view will be added to top-left corner
        params.x = 0;
        params.y = 100;

        //Add the view to the window
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mWindowManager.addView(mFloatingView, params);

It is almost okey but there is a problem. When I create overlay from service, System Back Button not working. If I use FLAG_NOT_FOCUSABLE System buttons; home, recents and back works as expected. I made some research at SO and find out that it is a security issue so I started to find alternative solutions and found this then changed my code

// Wrapper for intercepting System/Hardware key events
ViewGroup wrapper = new FrameLayout(this) {
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
            Log.v("Back", "Back Key");
            return true;
        }
        return super.dispatchKeyEvent(event);
    }
};

//Inflate the floating view layout we created
mFloatingView = LayoutInflater.from(this).inflate(R.layout.floating_widget_layout, wrapper);

//Add the view to the window.
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_PHONE,
        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
        PixelFormat.TRANSLUCENT);

//Specify the view position
params.gravity = Gravity.TOP | Gravity.START;        //Initially view will be added to top-left corner
params.x = 0;
params.y = 100;

//Add the view to the window
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mFloatingView, params);

Now I can get back press from my floating view but how can I pass this event(back press) to the foreground app(other app, not mine) ? I found this but it didn't help me

If someone can help me or at least give a clue, I will be appreciate

Summary

I have an overlay view, it needs to be focusable, but back press button not working when it is focusable how I can solve this?

Ridgeling answered 27/9, 2017 at 21:33 Comment(4)
have your solved this problem???same problem with meShipwright
Nope, Android does not allow to do this. So I added one step more. For getting input from user I opened new floating widget and block soft navigationRidgeling
@Ridgeling can you share you code ?Materi
Try this answer #37420171Dermatogen
K
-1

This tutorial seems to have a solution: https://www.geeksforgeeks.org/how-to-make-a-floating-window-application-in-android/

They set FLAG_NOT_FOCUSABLE initially so that the overlay doesn't interfere with system back function, etc. They set onTouch listener on the EditText and update the flag to FLAG_NOT_TOUCH_MODAL at that point so that text could be entered.

Kudu answered 24/3, 2021 at 6:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.