How to get focusable in a windowmanager view

1.8k views Asked by At

I use the WindowManager to create a view and add the view to the screen,but I cannot get the view focusable.I think I set the WindowManager.LayoutParams correct.

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);
    pushView = LayoutInflater.from(getApplicationContext()).inflate(
            R.layout.push, null);

    btn = (Button) findViewById(R.id.btn);
    cancelBtn = (Button) pushView.findViewById(R.id.cancelBtn);
    windowManager = getWindowManager();
    handler = new MyHandler();

    pushView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            System.out.println("onclick");
            windowManager.removeView(pushView);
        }
    });


    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT, 
                    WindowManager.LayoutParams.WRAP_CONTENT, 
                    WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 
                    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | 
                    WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                    PixelFormat.TRANSLUCENT);
            params.gravity = Gravity.TOP | Gravity.LEFT;
            params.y = 0;
            params.x = 0;

            windowManager.addView(pushView, params);

            new Timer().schedule(new TimerTask() {

                @Override
                public void run() {
                    Message msg = handler.obtainMessage();
                    handler.sendEmptyMessage(0x123);
                }
            }, 5000);

        }
    });

}

I want get the pushView'focusable to handle view's event.This problem has troubled me for a long time.Who can help me...

3

There are 3 answers

0
SLFly On

I have figured out the question.Because I use the TYPE_SYSTEM_OVERLAY. Window type: system overlay windows, which need to be displayed on top of everything else. These windows must not take input focus, or they will interfere with the keyguard. In multiuser systems shows only on the owning user's window.

So I cannot get the input focus.

3
shkschneider On

You simply need to remove the FLAG_NOT_FOCUSABLE flag:

params.flags = params.flags & ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
0
inlandfx On

This is probably 4 years too late but have you tried using WindowManager.LayoutParams.TYPE_SYSTEM_ALERT instead of TYPE_SYSTEM_OVERLAY.

The former doesn't seem to have the focus deprivation limiation