当我的应用程序尝试绘制系统覆盖图时,它会崩溃并告诉我2010窗口类型的权限被拒绝。但是,我要求用户在首次安装时在教程活动中授予ACTION_MANAGE_OVERLAY_PERMISSION
。此外,这只发生在API 26上。我测试了所有其他支持的SDK,我能够绘制一个没有崩溃或错误的叠加层。
我用过
Settings.canDrawOverlay(context);
然后返回true。因此,似乎系统知道授予许可但问题仍然存在。
以下是重叠代码:
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_driving, mRelativeLayout);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSLUCENT);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
layoutParams.flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
} else{
layoutParams.flags = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
layoutParams.flags = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
}
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
layoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
mRelativeLayout.setSystemUiVisibility(uiOptions);
if(Settings.canDrawOverlays(mContext)){
WindowManager windowManager = (WindowManager) mContext.getSystemService(WINDOW_SERVICE);
windowManager.addView(mRelativeLayout, layoutParams);
}
调用addView时发生崩溃。