Unable to add window Exception - android

I have this error in just android 5.1
Unable to add window android.view.ViewRootImpl$W#1df76e3 -- the specified window type is not valid
this is the code :
wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
params = new WindowManager.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT);
...
wm.addView(relativeLayout, params);

add this permission in AndroidManifest.
android.permission.SYSTEM_ALERT_WINDOW
on API >= 23 see

Related

How to change the WindowManager.LayoutParams' type programmatically?

I got a view that I am able to display on the lockscreen. But I want it to make it optional. Currently I use these params to achieve this:
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, PixelFormat.TRANSPARENT);
And for not showing it on the lockscreen I use
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, PixelFormat.TRANSPARENT);
Separately they work fine, but I want to change the type and flags programmatically. So I tried changing the WindowManager.LayoutParams with
params.flags &= ~WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
params.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
params.flags |= WindowManager.LayoutParams.TYPE_PHONE;
windowManager.updateViewLayout(myView,params);
to remove the type, remove the flag and set a new type,
but that doesn't seem to work. Does anyone know how to do this correctly?
You are setting type values into flag. Try this instead:
params.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
// type is no bit flag, so this should do it
params.type = WindowManager.LayoutParams.TYPE_PHONE;

Move popup window out of screen

Hello i have this code to show popup window...
final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
(width/100)*50, height, WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
parameters.gravity = Gravity.CENTER | Gravity.CENTER;
parameters.x = (width/100)*50;
parameters.y = 0;
But i need to show it out of screen...
When i set x position to more then windows size it's stuck at corner and not go over screen...
Thanks.
Edit: Because some people don't know what i mean.. i mean half outside screen not full..
You need to add one more flag which is flag_layout_no_limits
final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
(width/100)*50, height,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
parameters.gravity = Gravity.CENTER | Gravity.CENTER;
parameters.x = (width/2);
parameters.y = 0;

How to OverlayService Fullscreen?

wm = (WindowManager) getSystemService(WINDOW_SERVICE);
int type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
int flags = WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
int format = PixelFormat.TRANSLUCENT;
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
type, flags, format);
params.gravity = Gravity.RIGHT | Gravity.TOP;
wm.addView(lockScreenView, params);
I tried to use FLAG_LAYOUT_NO_LIMIT or FLAG_FULLSCREEN. But it's only available on devices that have a hard home button. They are unavailable in devices have a navigation bar or virtual keyboard.

WindowManager can not always draw on top

In my app I use a BroadcastReceiver to catch incoming calls. So when someone calls and in case that I have the phone number stored in my application's DB, I display a window with the name of the caller.
The problem is that in some devices this window is not displayed if the device's screen is off before the phone rings.(If the screen of the device is on, when the phone rings, the window is displayed).
I also delay the drawing of the window for 3 seconds, but this doesn't seem to work.
To display the window I use the following code, where mView is a RelativeLayout with a TextView. This code runs in a Service.
WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
dpToPx(72),
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
mParams.gravity = Gravity.TOP;
WindowManager mWindowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mView, mParams);
After several failing tries I found it.
I had to replace the flag TYPE_PHONEwith TYPE_SYSTEM_OVERLAY.
try this, this will surely work
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Layout_Flag = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Layout_Flag = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Layout_Flag = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
Layout_Flag,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER;
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
try {
mWindowManager.addView(windowView, params);

Add multiple flags to windowmanager

I have a WindowManager params set up like this:
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT );
However, this is causing an error. It's fine if I remove on of the flags, but I want both. How can I do this?
Use a bit-wise OR for your flags to combine them bitwise: flag1 | flag2
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT );

Categories

Resources