Android错误窗口导致后退按钮停止工作

时间:2015-09-01 03:05:14

标签: java android overlay android-windowmanager

我有一个应该覆盖通知栏并在触摸时执行操作的应用程序..

目前,我设法通过创建系统覆盖按钮来实现它:

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            statusBarHeight,
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                    | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
            PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.RIGHT | Gravity.TOP;

    WindowManager wm = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
    wm.addView(mView, params);

但是当我尝试单击设备后退按钮时,它不起作用。

请注意,我无法将TYPE_SYSTEM_ERROR更改为TYPE_SYSTEM_ALERT,因为它必须覆盖通知栏。

我也尝试添加标志FLAG_NOT_FOCUSABLE ..在这种情况下,后退按钮有效但onTouchEvent未被调用。

1 个答案:

答案 0 :(得分:0)

我做到了:

mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                Constant.NOTIFY_HEIGHT,
                WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        mParams.gravity = Gravity.CENTER | Gravity.TOP;

        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        mNotifyView = inflater.inflate(R.layout.notification_safety_count_down_layout, null);
        mNotifyTxt = (TextView) mNotifyView.findViewById(R.id.txt_notification_safety_count_down_time);

        mWindowManager.addView(mNotifyView, mParams);

        mNotifyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(SafetyCountDownService.this, DoroAssistanceAppActivity.class);
                intent.putExtra(Constant.CALL_FROM, Constant.CALL_FROM_NOTIFICATION);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);

            }
        });

请尝试!!!