title: How do I get MediaProjectionManager without disturbing the current foreground process, except to ask for permission?
body: <p>Problem: I have a screenshot app that uses a floating overlay service for controls, and screen cast API Media Project Manager to get access to the screen. Sometimes when a device is low on memory Android restarts the service, and I lose my media projection.</p>
  <p>The only way I know of to reacquire a new media projection is to re-open an Activity that requests the permissions, and that would be the end of it, except for one problem. Certain apps, particularly games, seem to listen for when they lose the foreground process, and pause, or otherwise reset. This is annoying.</p>
  <p>Here's my ideal scenerio. Service opens, if the user has selected &quot; checked don't ask me again&quot; in permission request dialog, it gets the media projection in a way that does not disturb the current foreground activity.</p>
  <p>How do I get a media projection manager without disturbing the current foreground process?</p>
  <p>Is there either a way to get media projection from a straight service, or a way to open activity in the background from a service?</p>
  <p>Currently I use this code in Activity to get the <code>MediaProjectionManager</code></p>
  <pre class="lang-java prettyprint-override"><code>@TargetApi(Build.VERSION_CODES.LOLLIPOP)
  protected void getScreenShotPermission() {
      if (isLollipopOrNewer) {
          mediaProjectionManager = (MediaProjectionManager) getContext().getSystemService(MEDIA_PROJECTION_SERVICE);
          startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), 1);
      }
  }
  
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == 1) {
          if (resultCode == Activity.RESULT_OK) {
              mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
              this.finish();
          }
      }
  }
  </code></pre>