悬浮窗

添加权限


实现

public class FloatView extends FrameLayout {

    private boolean isShown;
    private Context mContext;
    private View view;
    private WindowManager mWindowManager;

    public FloatView(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    public FloatView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FloatView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void showView() {
        view = LayoutInflater.from(mContext).inflate(R.layout.dialog, this);
        View cancel = view.findViewById(R.id.tv_cancel);
        cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                hidePopupWindow();
            }
        });
        View confirm = view.findViewById(R.id.tv_confirm);
        confirm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,MainActivity.class);
                mContext.startActivity(intent);
                hidePopupWindow();
            }
        });
        mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        //TYPE_TOAST仅适用于4.4+系统,假如要支持更低版本使用TYPE_SYSTEM_ALERT(需要在manifest中声明权限)
//        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_TOAST);
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        // 如果设置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,弹出的View收不到Back键的事件
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.flags |= WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
        //layoutParams.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; //no limit适用于超出屏幕的情况,若添加此flag需要增加边界检测逻辑
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.CENTER;
        // 不设置这个弹出框的透明遮罩显示为黑色
        layoutParams.format = PixelFormat.TRANSLUCENT;
        mWindowManager.addView(view, layoutParams);
        isShown = true;
    }

    /**
     * 隐藏弹出框
     */
    public void hidePopupWindow() {
        if (isShown && null != view) {
            mWindowManager.removeView(view);
            isShown = false;
        }

    }

}

窗口类型

FIRST_SYSTEM_WINDOW = 2000  第一个系统窗口
TYPE_STATUS_BAR = FIRST_SYSTEM_WINDOW   状态栏窗口
TYPE_SEARCH_BAR = FIRST_SYSTEM_WINDOW +1    搜索条窗口
TYPE_PHONE = FIRST_SYSTEM_WINDOW + 2    来电显示窗口
TYPE_SYSTEM_ALERT = FIRST_SYSTEM_WINDOW + 3 警告对话框
TYPE_KEYGUARD = FIRST_SYSTEM_WINDOW + 4 屏保
TYPE_TOAST = FIRST_SYSTEM_WINDOW + 5    Toast对应的窗口
TYPE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW + 6   系统覆盖窗口,需要显示在所有窗口之上
TYPE_PRIORITY_PHONE = FIRST_SYSTEM_WINDOW + 7   在屏幕保护下的来电显示窗口
TYPE_SYSTEM_DIALOG = FIRST_SYSTEM_WINDOW + 8    滑动状态条后出现的窗口
TYPE_KEYGUARD_DIALOG = FIRST_SYSTEM_WINDOW + 9  屏保弹出的对话框
TYPE_SYSTEM_ERROR = FIRST_SYSTEM_WINDOW + 10    系统错误窗口
TYPE_INPUT_METHOD = FIRST_SYSTEM_WINDOW + 11    输入法窗口
TYPE_INPUT_METHOD_DIALOG = FIRST_SYSTEM_WINDOW + 12 输入法中备选框对应的窗口
TYPE_WALLPAPER = FIRST_SYSTEM_WINDOW + 13   墙纸对应的窗口
TYPE_STATUS_BAR_PANEL = FIRST_SYSTEM_WINDOW + 14    滑动状态条后出现的窗口
TYPE_SECURE_SYSTEM_OVERLAY = FIRST_SYSTEM_WINDOW + 15   安全系统覆盖窗口,显示在所有窗口之上。
LAST_SYSTEM_WINDOW = 2999   最后一个系统窗口

你可能感兴趣的:(悬浮窗)