title: Play an audio file every X seconds Y times even when changing apps
body: <p>My app is supposed to receive time in seconds that determines how audio is played.</p>
  
  <p>Example: Play audio file x(length about 2 seconds) every 2 minutes all in all 16 times.</p>
  
  <p>It would work great if android didn't enjoy closing foreign apps and services so frequently, once you move to another app.</p>
  
  <p>My structure is like this:</p>
  
  <p><code>MainActivity</code> starts a new <code>MainActivityService</code> via <code>intent</code>.</p>
  
  <pre><code>gintent = new Intent(MainActivity.this,MainActivityService.class);
  gintent.putExtra(...);
  gintent.putExtra(...);
  ...
  startService(gintent);
  </code></pre>
  
  <p>Then this service runs the mentioned time being given to it by putExtra.</p>
  
  <p>The point is, android has all kinds of mechanisms to kill services and apps. So I have been trying to keep it up by using onSaveInstanceState in MainActivity</p>
  
  <pre><code>@Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  savedInstanceState.putBoolean(...);
  savedInstanceState.putInt(...);
  ...
  }
  </code></pre>
  
  <p><code>onRestoreInstanceState</code>, <code>isMyServiceRunning(MainActivityService.class)</code> and so forth, but I lost track with all the onResume and whatnot. And in the end, the service was still killed.</p>
  
  <p>I don't want to debug this back and forth. I want to know the gold standard how to do this.</p>