title: Using Intent Service in combination with Alarm Manager
body: <p>I have used the Alarm Manager to allow the user to schedule a certain task to repeat at a certain amount of time. In the context of the application, I have an Android game where people are able to schedule when to send their ships. The Alarm Manager is working fine, alarms get kicked off at the right time etc.</p>
  
  <p>What happens when an alarm is fired off (usually every hour and a bit), is that my <strong>IntentService</strong> will start communicating with the server to send the ships. This action may take 1 minute, but it can last up to 10 minutes. Even this all works fine. Alarms get fired, ships get sent, everyone happy. The problem arises at night, I go to sleep and expect when waking up that my ships have been sent all night long. Nope. Logging &amp; notifications show that the Alarms are fired correctly, but it appears that the IntentService is killed when it's communicating with the server.</p>
  
  <p>Possible cause for this is that I'm not looking at the game every once in a while like I do when I'm awake, and thus keep some form of process running which prevents the <strong>IntentService</strong> from being garbage collected.</p>
  
  <p>I've already tried a LOT of things to fix this. I used to work with a BroadCastReceiver, I used to spawn <strong>ASyncTasks</strong> in the <strong>IntentService</strong>, but I've since refactored my code to not use those things anymore as they're bad practice for Services.</p>
  
  <p>I have also checked this resource: <a href="http://techtej.blogspot.com.es/2011/03/android-thread-constructspart-4.html" rel="nofollow">http://techtej.blogspot.com.es/2011/03/android-thread-constructspart-4.html</a> but I'm still not sure if what I'm doing is the correct thing to handle this situation. I have placed some extensive logging for the next night to review in the morning but I'd like you guys' opinion over this.</p>
  
  <p>Oh I'm also requesting a WakeLock for the complete duration of the <strong>onHandleIntent</strong> function using:</p>
  
  <pre><code>PowerManager pm = (PowerManager) c.getSystemService(Context.POWER_SERVICE);                                         
  this.wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "MyApplicationWakeLock");
  </code></pre>
  
  <p>My IntentService:</p>
  
  <pre><code>public class ScheduledRequestService extends IntentService {
  
      public ScheduledRequestService() {
          super("ScheduledRequestService");
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startID){
          Logger.d("OnStartCommand!");
          return super.onStartCommand(intent, flags, startID);
      }
  
      @Override
      protected void onHandleIntent(Intent intent) {
          ScheduledRequest request = (ScheduledRequest) intent.getExtras().getSerializable("request");
          // This function will start a lot of client &lt;-&gt; server communication
          request.onExecute(this, intent);
      }
  
      @Override
      public void onDestroy(){
          Logger.d("OnDestroy!");
          super.onDestroy();
      }
  }
  </code></pre>
  
  <p>So again, my question is; am I structuring this correctly? Should I use a normal <strong>Service</strong> instead of an <strong>IntentService</strong> ? Can my IntentService get garbage collected while it is handling an <strong>Intent</strong> (I think I read that this is possible)? </p>