android 悬浮窗口 权限 (ACTION_MANAGE_OVERLAY_PERMISSION)
程序员文章站
2022-05-14 21:50:58
...
在开发悬浮球app的过程中,当第一次打开肯定会遇到权限问题
Caused by: android.view.WindowManager$BadTokenException: Unable to add window
[email protected]2886501 -- permission denied for window type 2007
at android.view.ViewRootImpl.setView(ViewRootImpl.java:703)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at com.pinecone.d2dinfofloatball.FloatBallManager.addBallView(FloatBallManager.java:88)
2007 代表 允许出现在其他应用上 ,所以需要将其打开
然后再次打开就,windowmanager 绘制 悬浮窗口的时候就没问题
其原理,检查app的uid
mOverlayState =
mOverlayBridge.getOverlayInfo(mPackageName,mPackageInfo.applicationInfo.uid);
而在开机的时候PMS 会添加默认开机不检查的进程
mSettings.addSharedUserLPw("android.uid.system", Process.SYSTEM_UID,
ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
mSettings.addSharedUserLPw("android.uid.phone", RADIO_UID,
ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
mSettings.addSharedUserLPw("android.uid.log", LOG_UID,
ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
mSettings.addSharedUserLPw("android.uid.nfc", NFC_UID,
ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
mSettings.addSharedUserLPw("android.uid.bluetooth", BLUETOOTH_UID,
ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
mSettings.addSharedUserLPw("android.uid.shell", SHELL_UID,
ApplicationInfo.FLAG_SYSTEM, ApplicationInfo.PRIVATE_FLAG_PRIVILEGED);
其他的permission 是需要在manifest中配置
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
因为在绘制时候添加顶层view 布局
mFBParams.type = LayoutParams.TYPE_SYSTEM_ALERT | LayoutParams.TYPE_SYSTEM_OVERLAY;//顶层的View ,就要用这两个type
上一篇: PHP学习笔记之数组值及数组遍历和排序
下一篇: 软件工程师笔试题