title: Service.startForeground() not allowed due to bg restriction
body: <p>I'm trying to run a <code>foregroundService</code> in android 8 and later. It starts successfully and the foreground sticky notification is showing when app is in foreground or device is not idle. Problem is once device goes idle, Android decide to kill the service showing this log</p>
  
  <pre><code>Stopping service due to app idle
  </code></pre>
  
  <p>According to docs as long as there is a notification visible to user service should rarely be killed :( Anyway i setup an alarm that fires every 15 minutes and tries to start the service again if its not running but it fails to start foreground service logging following message</p>
  
  <pre><code>Service.startForeground() not allowed due to bg restriction
  </code></pre>
  
  <p>With some searching on google: I figured it has something to do with android AppOps management. Apparently there is a permission <code>RUN_ANY_IN_BACKGROUND</code> that should be <code>allow</code> but its actually <code>ignore</code> in my case. I check that using </p>
  
  <pre><code>adb shell cmd get package.name RUN_ANY_IN_BACKGROUND
  </code></pre>
  
  <p>Just for the sake of testing i set it to <code>allow</code> again manually. But after some time when app goes idle OS set it to ignore again and service is stopped again.</p>
  
  <p><strong>Anyway i can check if i'm violating some limitations that force this <code>RUN_ANY_IN_BACKGROUND</code> flag to ignore</strong>?</p>
  
  <p>For some context: The service is suppose to detect user's sleep, so it has to run all night long from user's sleep time until wake time.
  I start service by calling <code>context.startForegroundService(intent)</code> and then in <code>onStartCommand</code> method i call <code>startForeground</code> passing notification. <code>onStartCommand</code> method always return <code>START_REDELIVER_INTENT</code> constant.</p>