Android-仿QQ/微信-全局新消息提示 仿微信悬浮通知栏/横幅通知

	private WindowManager wm;
    private boolean showWm = true;//默认是应该显示悬浮通知栏
    private WindowManager.LayoutParams params;
    private View view;

    private void initWindowManager() {
        wm = (WindowManager) getApplicationContext().getSystemService(
                Context.WINDOW_SERVICE);
        params = new WindowManager.LayoutParams();
        //注意是TYPE_SYSTEM_ERROR而不是TYPE_SYSTEM_ALERT
        //前面有SYSTEM才可以遮挡状态栏,不然的话只能在状态栏下显示通知栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        } else {
            params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        }
        params.format = PixelFormat.TRANSLUCENT;
        //设置必须触摸通知栏才可以关掉
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_FULLSCREEN
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

        // 设置通知栏的长和宽
        params.width = wm.getDefaultDisplay().getWidth() - 60;
        params.height = DimenUtils.dp2px(this, 90);
        params.alpha = 1f;
        params.x = 0;
        params.y = ScreenUtils.getStatusBarHeight(this) / 2;
        params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
    }

    private void createFloatView(String title, String content) {
        //准备Window要添加的View
        if (view == null) {
            view = LayoutInflater.from(this).inflate(R.layout.dialog_message_notify, null);
        }
        view.setOnTouchListener(this);
        TextView titleTv = view.findViewById(R.id.title);
        titleTv.setText(title);
        TextView contentTv = view.findViewById(R.id.content);
        contentTv.setText(content);
        //在这里你可以解析你的自定义的布局成一个View
        if (showWm) {
            wm.addView(view, params);
            showWm = false;
            animShow();
        } else {
            wm.updateViewLayout(view, params);
        }
    }

    /**
     * 动画,从顶部弹出
     */
    private void animShow() {
        //使用动画从顶部弹出
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", -DimenUtils.dp2px(this, 90), 0);
        animator.setDuration(400);
        animator.start();
    }

    /**
     * 动画,从顶部收回
     */
    private void animDismiss() {
        if (view == null || view.getParent() == null) {
            return;
        }
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", view.getTranslationY(), -view.getMeasuredHeight());
        animator.setDuration(400);
        animator.start();
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                //移除HeaderToast  (一定要在动画结束的时候移除,不然下次进来的时候由于wm里边已经有控件了,所以会导致卡死)
                if (null != view && null != view.getParent()) {
                    wm.removeView(view);
                    showWm = true;
                    view = null;
                }
            }
        });
    }

    boolean isClick;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isClick = true;
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                if (isClick) {
                    isClick = false;
//                wm.removeViewImmediate(view);
//                view = null;
                    animDismiss();
                    startActivity(new Intent(this, MainActivity.class));
                }
                break;
            default:
                break;
        }
        return true;
    }

Android 悬浮窗在状态栏之上不影响其他操作及悬浮窗自身控制

 param.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR; // 系统提示类型,重要
 params.format = PixelFormat.TRANSLUCENT;
 params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                 | WindowManager.LayoutParams.FLAG_FULLSCREEN
                 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

Android permission denied for window type

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if(!Settings.canDrawOverlays(getApplicationContext())) {
                //启动Activity让用户授权
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                intent.setData(Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent,100);
            }
        }
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 100){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Settings.canDrawOverlays(this)) {
                    WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
                    WindowManager.LayoutParams params = new WindowManager.LayoutParams();
                    params.type = WindowManager.LayoutParams.TYPE_PHONE;
                    params.format = PixelFormat.RGBA_8888;
                    windowManager.addView(view,params);
                }else {
                    Toast.makeText(this,"ACTION_MANAGE_OVERLAY_PERMISSION权限已被拒绝",Toast.LENGTH_SHORT).show();;
                }
            }

        }

你可能感兴趣的:(android,微信)