WindowManager 在wm.addView(phoneView, params)报错Unable to add window android.view.ViewRootImpl$W@a3f49

WindowManager 在wm.addView(phoneView, params)报错Unable to add window android.view.ViewRootImpl$W@a3f49-- permission denied for window type 2002

除了加权限


还要有如下判断

inflate= LayoutInflater.from(getApplicationContext());
        wm = (WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT > 25) {
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }
//        params.type = WindowManager.LayoutParams.TYPE_PHONE;
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_DITHER;
        params.gravity= Gravity.CENTER;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = 700;
        params.format = PixelFormat.RGBA_8888;
        phoneView=inflate.inflate(R.layout.phone_alert,null);
        wm.addView(phoneView, params);
//        Log.e("TAG","响铃:来电号码"+incomingNumber);
        Log.e("MyService","响铃:======"+Thread.currentThread().getName());
        //输出来电号码

主要代码是

 if (Build.VERSION.SDK_INT > 25) {
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }

你可能感兴趣的:(Java,Android)