title: Android system overlay not shown outside my app
body: <p>I need to show a system overlay window on top of any running apps. It works fine, but only when my app's activity is in foreground. Here is the code I use:</p>
  
  <pre><code>params = new WindowManager.LayoutParams(
                      WindowManager.LayoutParams.WRAP_CONTENT,
                      WindowManager.LayoutParams.WRAP_CONTENT,
                      WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                      WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 
                      WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
                      WindowManager.LayoutParams.FLAG_DIM_BEHIND |
                      WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | 
                      WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                      WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                      WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,
                      PixelFormat.TRANSLUCENT);
  
  params.gravity = Gravity.CENTER | Gravity.TOP;
  params.dimAmount = 0.3f;
  wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
  
      mainAlertView = View.inflate(ctx, R.layout.system_alert_view, null);
  
      wm.addView(mainAlertView, params);
  </code></pre>
  
  <p>The view added to WindowManager contains empty LinearLayout to which I add child views at runtime:</p>
  
  <pre><code>    mainAlertLayout.addView(childView);
  </code></pre>
  
  <p>Manifest xml has <code>SYSTEM_ALERT_WINDOW</code> permission defined:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/&gt;
  </code></pre>
  
  <p>It all works magnificently while my activity is running in foreground. As soon as I switch to any other app or home screen no overlay is shown. I debugged the code, and it indeed is running wm.addView(), and its not throwing any exceptions. I also played around with LayoutParams flags (removed KEEP_SCREEN_ON and related etc), but nothing made any difference.</p>
  
  <p>Android 4.4.2 fwiw</p>