在浮动窗口中无法获取listview点击后台应用程序的事件

时间:2014-11-29 18:47:37

标签: android android-windowmanager

我创建了浮动按钮,能够保持所需位置,如图所示,以及我启动设置应用程序时访问其他应用程序。 但它阻止了后退键和列表视图项目点击后台应用程序,如图所示 如果我们考虑wifi选项能够打开或关闭按钮但不能进入wifi选项。 请帮助解决这个问题

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        show(0);
        return START_NOT_STICKY;
    }
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            PixelFormat.TRANSLUCENT);
    private int width;
    private int height;
    public final synchronized void show(final int id) {
        width = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
        height = ((Activity) context).getWindowManager().getDefaultDisplay().getHeight();
        final Button button = new Button(context);
        button.setText("I'm a button!");
        button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        button.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.performClick();

                if(event.getAction() == MotionEvent.ACTION_MOVE){
                    int pos[] = new int[2];
                    button.getLocationOnScreen(pos);
                    Log.i(MainActivity.TAG, "onTouch"+event.getRawX()+" :: "+width+" :: "+pos[0]);
                    Log.d(MainActivity.TAG, "onTouch"+event.getRawY()+" :: "+height+" :: "+pos[1]);
                    params = new WindowManager.LayoutParams(
                            WindowManager.LayoutParams.WRAP_CONTENT,
                            WindowManager.LayoutParams.WRAP_CONTENT,
                            (int)(event.getRawX())-(width/2), 
                            (int)(event.getRawY())-(height/2),
                            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                            PixelFormat.TRANSLUCENT);


                    mWindowManager.updateViewLayout(button, params);
                                }
                return true;
            }
        });
        mWindowManager.addView(button, params);
    }

enter image description here

1 个答案:

答案 0 :(得分:1)

需要添加标记0WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE然后它会正常工作,如下面的代码段

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.performClick();
        if(event.getAction() == MotionEvent.ACTION_MOVE){
            params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    (int)(event.getRawX())-(width/2), 
                    (int)(event.getRawY())-(height/2),
                    0,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSLUCENT);
            mWindowManager.updateViewLayout(floatingView, params);
            return true;
        }
        return true;
    }