title: my Android service won't keep running
body: <p>I have a service that performs an AsyncTask which calls itself after each completion. As you'll see below, I am starting my service in the foreground. It starts successfully and keeps running as intended while I have it plugged into my computer and spitting output to LogCat. I know this because to test, I have my AsyncTask loop spitting out a notification every 5 minutes. However, when I unplug it from my computer, the notifications don't come! It's as if the service just completely stops after I start it!</p>
  
  <p>NOTE: My service is a regular service, not an IntentService.</p>
  
  <p>Here is my onStartCommand...</p>
  
  <pre><code>@SuppressWarnings("deprecation")
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      getData(intent);
      self = this;
  
      // Enter foreground state
      String title = "Service started.";
      String subject = "Service is running.";
      String body = "Monitoring...";
      Notification notification = new Notification(R.drawable.ic_launcher, title,
              System.currentTimeMillis());
      if(notificationSounds)
          notification.defaults |= Notification.DEFAULT_SOUND;
      else
          notification.sound = null;
      Intent notificationIntent = new Intent(this, MainActivity3.class);
      PendingIntent pendIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
      notification.setLatestEventInfo(this, subject, body, pendIntent);
      startForeground(1500, notification);
  
      new BatteryLifeTask(appContext).execute();
      return START_NOT_STICKY;
  }
  </code></pre>
  
  <p>Here is my AsyncTask:</p>
  
  <pre><code>// Looping AsyncTask for continuous mode
  private class BatteryLifeTask extends AsyncTask&lt;Void, Void, Void&gt; {
  
      // Member variables
      Context appContext;
      int batteryPct0;
  
      public BatteryLifeTask(Context context) {
          super();
          appContext = context;
      }
  
      protected Void doInBackground(Void... params) {
          System.out.println("Entering doInBackground");
          // Get the initial battery level
          batteryPct0 = getBatteryPercent();
          System.out.println("Initial battery percent: " + batteryPct0);
  
          // Check time
          Calendar c = Calendar.getInstance();
          Date dateNow = c.getTime();
          // getTime returns ms, need minutes. 60000ms in a minute.
          long currTime = dateNow.getTime() / 60000;
  
          if(currTime &gt;= timeToUse){
              finished = true;
              stopSelf();
          }
  
          System.out.println("Leaving doInBackground");
          return null;
      }
  
      @Override
      protected void onPostExecute(Void aVoid) {
          super.onPostExecute(aVoid);
          if(!finished) {
              int waitTime = 60000 * interval; // 1 minute is 60000 miliseconds
              System.out.println("Entering postExecute. waitTime is " + waitTime);
              Runnable r = new Runnable() {
                  @Override
                  public void run() {
                      if(!finished) { // In case postDelayed is pending, avoids extra notification
                          System.out.println("An interval has passed.");
                          calculateHelper(batteryPct0);
                          new BatteryLifeTask(appContext).execute();
                      }
                  }
              };
              Handler h = new Handler();
              h.postDelayed(r, waitTime);
          }
      }
  }
  </code></pre>
  
  <p>And here is my code for creating notifications:</p>
  
  <pre><code>// Method for creating a notification
  @SuppressWarnings("deprecation")
  void notify0(int id, String title, String subject, String body, boolean playSound){
      NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      Notification notify = new Notification(android.R.drawable.
      PendingIntent pending = PendingIntent.getActivity(
              getApplicationContext(), 0, new Intent(), 0);
      notify.setLatestEventInfo(getApplicationContext(), subject, body, pending);
      if(playSound)
          notify.defaults |= Notification.DEFAULT_SOUND;
      else
          notify.sound = null;
  
      // Cancel running notification if exists
      NM.cancel(id);
  
      // Push notification
      NM.notify(id, notify);
  }
  </code></pre>
  
  <p><br/>
  <br/>
  Can anyone help me? This is driving me insane! My app works PERFECTLY when plugged in and hooked up to USB debugging. But when unplugged, the service seems to completely halt and do nothing.</p>