Android: Unable to add window. Permission denied for this window type
For what should be completely obvious reasons, ordinary Apps are not allowed to create arbitrary windows on top of the lock screen. What do you think I could do if I created a window on your lockscreen that could perfectly imitate the real lockscreen so you couldn't tell the difference?
The technical reason for your error is the use of the TYPE_KEYGUARD_DIALOG
flag - it requires android.permission.INTERNAL_SYSTEM_WINDOW
which is a signature-level permission. This means that only Apps signed with the same certificate as the creator of the permission can use it.
The creator of android.permission.INTERNAL_SYSTEM_WINDOW
is the Android system itself, so unless your App is part of the OS, you don't stand a chance.
There are well defined and well documented ways of notifying the user of information from the lockscreen. You can create customised notifications which show on the lockscreen and the user can interact with them.
Unable to add window -- permission denied for this window type
This question has been solved by myself
UPDATE
make sure you have added permission in the manifest.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
put this on onCreate or whatever you want to askPermission()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
askPermission()
}
Method:
@TargetApi(23)
public void askPermission() {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, SYSTEM_ALERT_WINDOW_PERMISSION);
}
Unable to add window - windowmanager permission denied for this window type 2003 or 2006
I've solved the problem. We need some permissions when working with Android M and above. You can fix this problem with "draw over other apps" permission.
Add to manifest
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
WindowManager
windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
surfaceView = new SurfaceView(this);
ViewGroup.LayoutParams layoutParams = new WindowManager.LayoutParams(1, 1,
Build.VERSION.SDK_INT < Build.VERSION_CODES.O ?
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY :
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
windowManager.addView(surfaceView, layoutParams);
surfaceView.getHolder().addCallback(this);
Add to your codes (onCreate)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 0);
}
}
Permission denied for window type 2002 in Android Studio
This is occurring because the targetSdkVersion in the example and your targetSdkVersion are different. Use the flag TYPE_APPLICATION_OVERLAY instead of TYPE_PHONE in WindowManager.LayoutParams:
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
Unable to add window android.view.ViewRootImpl$W@3660b6 -- permission denied for window type 2038
Maybe you haven't requested android.permission.SYSTEM_ALERT_WINDOW
permission in your manifest.
You should add this to AndoridManifest.xml
in your application under the manifest
tag:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
and in run time you should till the user to grant you the permission:
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
intent.setData(Uri.parse("package:" + context.getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Unable to add window android.view.ViewRoot$W@44da9bc0 -- permission denied for this window type
Try using this permission in AndroidManifest.
android.permission.SYSTEM_ALERT_WINDOW
on API >= 23 see
Permission denied for window type 2038
Android Oreo and above severely limit which overlay types you're allowed to use, as you have seen. Oreo introduces the new TYPE_APPLICATION_OVERLAY
constant for apps to use, while deprecating and disallowing the old constants.
However, TYPE_APPLICATION_OVERLAY
doesn't exist in Nougat and below, and so attempting to use it there will cause a SecurityException.
Replace
mProgressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
With
mProgressDialog.getWindow().setType(
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
else
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
As for why it's still crashing on Oreo and above with TYPE_APPLICATION_OVERLAY
, the SYSTEM_ALERT_WINDOW
permission isn't a normal runtime permission, and doesn't show under the "Permissions" section in the app info. The user-facing name for it is "Draw over other apps" (usually), and it's under the general App Info page or under Settings>>Apps>>Special Access.
You can't grant it programmatically like with runtime permissions, where you have a simple dialog to grant it. Instead, you need to check if the permission is granted, and if it isn't, launch the Settings page to allow the user to grant it: How to programmatically grant the "draw over other apps" permission in android?
However, I don't really see why a simple progress dialog needs to be a full-blown overlay. In fact, the ProgressDialog has been completely deprecated. You should consider migrating to a simple ProgressBar or, if you want to keep the ProgressDialog, removing the setType()
line.
Android: permission denied for window type 2038 using TYPE_APPLICATION_OVERLAY
I had the exactly same issue. I guess you should differentiate the target (before and after Oreo)
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
Related Topics
Android.Util.Androidruntimeexception: You Cannot Combine Swipe Dismissal and the Action Bar
Good Way of Getting the User's Location in Android
Android Recyclerview Gridlayoutmanager Column Spacing
What Is a "Bundle" in an Android Application
Android Transparent Status Bar and Actionbar
Bad Image Quality After Resizing/Scaling Bitmap
How Are Android Touch Events Delivered
Android: Remove Left Margin from Actionbar's Custom Layout
How to Determine Mime Type of File in Android
Custom Notification Layouts and Text Colors
Html.Fromhtml Deprecated in Android N
Single Textview with Multiple Colored Text
How to Center Align the Actionbar Title in Android
Launch Facebook App from Other App
Determine If Android App Is Being Used For the First Time
How to Inflate One View with a Layout