Reputation: 1934
I have an app that has a widget that will allow chat to be available as a floating widget, kind of like FB has. It works perfectly on lower versions of Android, but for some reason it will not work on Android 8.0. I have read through the changes in permissions and in my AndroidManifest.xml file I have both permissions declared...
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.TYPE_APPLICATION_OVERLAY"/>
Then in my activity I am checking for the permission and if it is granted, it will attempt to add the new view...
if(Settings.canDrawOverlays(this)) {
initializeChatHeadsView();
}
initializeChatHeadsView, just adds the layout to the window. I have enabled the permission, I can even see that it is enabled if I go into the settings on the device, I can see my app listed in the "Display over other apps" permissions page, so it does have permissions, but every time I try to add the view the app crashes and I get the message...
Unable to add window android.view.ViewRootImpl$W@f6948a0 -- permission denied for window type 2002
I don't understand why it isn't working. The manifest had both permissions for older and newer versions, the system checks specifically for "canDrawOverlays" and it works great on every single emulator I have, except for the ones that are running Android 8.0.
I am more than happy to show more code if it is needed. Any help would be extremely appreciated. Thank you.
Upvotes: 3
Views: 3476
Reputation: 1934
I found the issue. If anyone else is having an issue with apps that draw over other apps in android 8.0, I had to change the way I was building the window....
int layoutType;
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
layoutType = WindowManager.LayoutParams.TYPE_PHONE;
} else {
layoutType = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
}
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
layoutType,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
And now it works perfectly.
Upvotes: 6