Android permission denied for window type 2002

最近在做一个悬浮窗功能,一运行就报如下错误:

Process: com.afscope.powerassistant, PID: 4239
    java.lang.RuntimeException: Unable to create service com.afscope.powerassistant.MonitorService: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@98e9d5e -- permission denied for window type 2002
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3878)
        at android.app.ActivityThread.-wrap5(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1989)
        at android.os.Handler.dispatchMessage(Handler.java:108)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7425)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
     Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@98e9d5e -- permission denied for window type 2002
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:908)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:372)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:128)
        at com.afscope.powerassistant.MonitorService.onCreate(MonitorService.java:66)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3858)
        at android.app.ActivityThread.-wrap5(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1989) 
        at android.os.Handler.dispatchMessage(Handler.java:108) 
        at android.os.Looper.loop(Looper.java:166) 
        at android.app.ActivityThread.main(ActivityThread.java:7425) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) 

工程中代码如下:

DisplayMetrics dm = getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
mLayoutParams = new WindowManager.LayoutParams();

mLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL  | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
mLayoutParams.width = screenWidth/2;
mLayoutParams.height = screenHeight/2;
mWindowManager.addView(mRecorderView, mLayoutParams);
看到报错信息中的

permission denied for window type 2002

第一反应以为是权限问题,然后加上权限


结果运行还是崩溃,原来经过我多方找资料才发现

Android O之前的系统中申请了该权限后,再给对应的window设置

  1. WindowManager.LayoutParams params = new WindowManager.LayoutParams();
  2. params.type = WindowManager.LayoutParams.TYPE_PHONE;
  3. 悬浮窗口就可以显示出来。

但是在Android O的系统中,google规定申请

android.permission.SYSTEM_ALERT_WINDOW
权限的应用需要给悬浮窗口设置如下type:
  1. WindowManager.LayoutParams params = new WindowManager.LayoutParams();
  2. params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
悬浮窗口才能显示出来, TYPE_APPLICATION_OVERLAY”是重点

你可能感兴趣的:(Android permission denied for window type 2002)