title: Service crashing and restarting
body: <p>There are several questions about it but I always read the same thing: "the service will be killed if the system need resources" or "you can't build an service that runs forever because the more it runs in background, more susceptible it is to the system kills it" and etc.</p>
  
  <p>The problem I'm facing is: My service runs fine and as it is expected, if I run my app then exit it my service is still running, but when I kill my app (by going to the "recent apps" and swype it away) the service stops. In this moment, if I go to the Settings >> aplications >> running I'll see that the service is restarting. After a while, it goes back and my Service run with no problem.</p>
  
  <p>I google it and I find some things I could do but lets see my code first:</p>
  
  <p>I start my service by this way (after a button click):</p>
  
  <pre><code>Intent intent = new Intent (MainActivity.this, MyService.class);
  startService(intent);
  </code></pre>
  
  <p>I also have 3 Integers I put in extra, so I have something like this:</p>
  
  <pre><code>final Integer i, i2, i3;
  i = 5; //for example
  i2 = 10; //for example
  i3 = 15; //for example
  final Intent intent = new Intent (MainActivity.this, MyService.class);
  intent.putExtra("INTEGER1", i);
  intent.putExtra("INTEGER2", i2);
  intent.putExtra("INTEGER3", i3);
  startService(intent);
  </code></pre>
  
  <p>In MyService I have the folloywing:</p>
  
  <pre><code>public class MyService extends Service
  {
  
    AlarmManager am;
    BroadcastReceiver br;
    PendingIntent pi;
    Integer i, i2, i3;
  
    @Override
    public void onCreate()
    {
      super.onCreate();
      am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
      pi = PendingIntent.getBroadcast(this, 0, new Intent("anyany"); 0) //Why those zeros?
  
      br = new BroadcastReceiver ()
      {
        public void onReceive (Context context, Intent i) {
          new thread(new Runnable()
          {
            public void run()
            {
              //do something
            }
          }).start();
        }
      };
    }
  
    @Override
    public void onStartCommand(Intent intent, int flags, int startId)
    {
      super.onStartCommand(intent, flags, startId);
      try
      {
        i = intent.getIntExtra("INTENT1", 0) // I don't understant yet why this zero are here
        i2 = intent.getIntExtra("INTENT2", 0)
        i3 = intent.getIntExtra("INTENT3", 0);
      }
      catch(NullPointerException e) {}
  
      this.registerReceiver(br, new IntentFilter("anyany"));
  
      new thread(new Runnable()
      {
      public void run()
        {
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock. elapsedRealtime() + i*1000, i2*1000, pi);
        }
      }).start();
    return START_REDELIVER_INTENT; //so I can get my Extra even with my Activity closed
  }
  </code></pre>
  
  <p>My onDestroy:</p>
  
  <pre><code>@Override
  public void onDestroy()
  {
    unregisterReceiver(br);
    super.onDestroy();
  }
  </code></pre>
  
  <p>I also have onBind() method (without @Override), but it returns null.
  I google a lot and I tried to run the service in foreground, so I did this (inside de onStartCommand):</p>
  
  <pre><code>Notification n = new Notification(R.drawable.ic_laucher), getText(R.string.app_name), System.currentTimeMillis());
  PendingIntent npi = PendingIntent.getActivity(this, MainActivity.class);
  n.setLatestEventInfo(this, getText(R.string.notification_title), getText(R.string.notification_message), npi);
  startForeground(3563, n);
  </code></pre>
  
  <p>My notification appears and when I click on it my app runs, but the problem with my service wasn't fixed (I believe it still not run on foreground). The notification is restarted too.</p>
  
  <p>I also deleted the Try catch and I define a value for the integers (so I didn't use the getIntExtra() method), but nothing changed</p>
  
  <p>After several tests I tried to see the logs, when I kill my App I have the following message: Scheduling restart of crashed service.</p>
  
  <p>So, for some reason my service crash when my MainActivity dies, why? The intention here is not to transform the service in a god that can not be killed (I don't think it is impossible at all, the WhatsApp are running for 105 hours !) but prevent my Service to not being crashed after my App dies.</p>
  
  <p>I don't know if this'll help but this is what I add on my Manifest.xml</p>
  
  <pre><code>&lt;Activity android:name = ".MyService"/&gt;
  &lt;service android:name ="Myservice" android:enabled="true" android: exported="false"/&gt;
  &lt;uses-permission android:name="android.permission.WAKE_LOCK"/&gt;
  </code></pre>
  
  <p>Min API = 9, target API = 17.
  Size of the Service when running: about 3MB.</p>
  
  <p>Hope I was clear and sorry for my English.</p>
  
  <p>PS: the entire code are running as expected, so if you see any sintax error fell free to edit it.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>If I add <code>android:isolatedProcess="true"</code> in the <code>&lt;service&gt;</code> in AndroidManifest.xml I receive this error in logCat: <code>java.lang.RuntimeException: Unable to create a service in com.mycompany.myapp.myservice: java.lang.SecurityException: Isolated process not allow ed to call getIntentSender</code></p>
  
  <p>When I start my service using this, the MainActivity does not show any erros, only the service crashes.</p>