Android: permission denied for window type 2038 using TYPE_APPLICATION_OVERLAY

I am trying to create a view that is above other applications:

WindowManager.LayoutParams paramsDirectorView = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); 

I looked through the other answers and found the following things for "drawing applications":

  • I have android.permission.SYSTEM_ALERT_WINDOW in the manifest
  • I do Settings.canDrawOverlays (this), checking which comes back true.
  • I have done everything that is allowed here for the window type

I am still getting the "Failure Permission for Window Type 2038" error. I'm currently using TYPE_PHONE and it works, but it is deprecated and says use TYPE_APPLICATION_OVERLAY. Can someone keep an eye on this, as the answer of TYPE_PHONE is not really a resolution, but a “fix” solution that is deprecated in Android O.

I am running on Android 7.1.2

android.view.WindowManager $ BadTokenException: cannot add window android.view.ViewRootImpl$W@1f47e89 - permission is denied for window type 2038 on android.app.ActivityThread.handleServiceArgs (ActivityThread.java data 344) in android.app.ActivityThread.-wrap21 (ActivityThread.java ) in android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1583) on android.os.Handler.dispatchMessage (Handler.java:102) at android.os.Looper.loop (Looper.java:154) at android .app.ActivityThread.main (ActivityThread.java:6121) in java.lang.reflect.Method.invoke (native method) in com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:889) at com .android.internal.os.ZygoteInit.main (ZygoteInit.java:779) Caused by: android.view.WindowManager $ BadTokenException: cannot add window android.view.ViewRootImpl$W@1f47e89 - permission denied for window type 2038 in android.view.Vi ewRootImpl.setView (ViewRootImpl.java:703) in android.view.WindowManagerGlobal.addView (WindowManagerGlobal.javahaps42) in android.view.WindowManagerImpl.addView (WindowManagerImpl.java:93) in HeadService.TwoViewManager. (TwoViewManager.java:99) in HeadService.UIHeadService.onStartCommand (UIHeadService.java:65) in android.app.ActivityThread.handleServiceArgs (ActivityThread.javahaps327) in android.app.ActivityThread.-wrap21 (ActivityThread.java) android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1583) on android.os.Handler.dispatchMessage (Handler.java:102) at android.os.Looper.loop (Looper.java:154) at android.app .ActivityThread.main (ActivityThread.java:6121) in java.lang.reflect.Method.invoke (native method) in com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:889) at com.android .internal.os.ZygoteInit.main (ZygoteInit.java:779)

+5
source share
5 answers

I had exactly the same problem. I think you should distinguish between the goal (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); 
+12
source
 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 

I had exactly the same problem in the service class (before and after Marshmallow).

 if (Build.VERSION.SDK_INT >= 23) { if (!Settings.canDrawOverlays(this)) { Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())); startActivityForResult(intent, 1234); } } else { startService(new Intent(SplashActivity.this, CheckServicesForApps.class)); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1234) { startService(new Intent(SplashActivity.this, CheckServicesForApps.class)); } } public class CheckServicesForApps extends Service { private Context context = null; @Override public void onCreate() { super.onCreate(); ImageView imageView = new ImageView(context); imageView.setVisibility(View.GONE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { try { windowManager = (WindowManager)getSystemService(WINDOW_SERVICE); //here is all the science of params final 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_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, PixelFormat.TRANSLUCENT ); windowManager.addView(imageView, params); hand=new Handler(); } catch (Exception e) { hand=new Handler(); e.printStackTrace(); } }else{ windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); final WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); params.gravity = Gravity.TOP | Gravity.CENTER; params.x = ((getApplicationContext().getResources().getDisplayMetrics().widthPixels) / 2); params.y = ((getApplicationContext().getResources().getDisplayMetrics().heightPixels) / 2); windowManager.addView(imageView, params); } } @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { /* We want this service to continue running until it is explicitly * stopped, so return sticky. */ return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); if (imageView != null) { try { windowManager.removeView(imageView); } catch (Exception e) { e.printStackTrace(); } } /**** added to fix the bug of view not attached to window manager ****/ } } 
+1
source

Try changing WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY to WindowManager.LayoutParams.TYPE_PHONE ?

0
source

Did you request permission at runtime by invoking the following intent?

 private void requestOverlayPermission() { if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) { return; } Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); myIntent.setData(Uri.parse("package:" + getPackageName())); startActivityForResult(myIntent, APP_PERMISSIONS); } 

Then in onActivityResult () check if Settings.canDrawOverlays (this) is true, otherwise request permission again by calling the method above.

0
source

Source SYSTEM_ALERT_WINDOW String SYSTEM_ALERT_WINDOW Allows an application to create windows using the TYPE_APPLICATION_OVERLAY type displayed on top of all other applications. Very few applications must use this permission; these windows are intended for interaction at the system level with the user.

Note. If the application targets API level 23 or higher, the application user must explicitly grant this permission to the application through the permission management screen. The application requests user approval by sending an intent with the ACTION_MANAGE_OVERLAY_PERMISSION action. An application can check if it has this permission by calling Settings.canDrawOverlays ().

0
source

Source: https://habr.com/ru/post/1271776/


All Articles