title: how to schedule a background service between certain hours android
body: <p>I have a service that tracks the location of the user periodically.
  I want this service to run only during working hours, so I want it to start at 9h and to stop at 18h.</p>
  
  <p>I'm starting the service with an <code>AlarmManager</code> with this code</p>
  
  <pre><code>        Intent syncIntent = new Intent(AlarmService.this, TrackingService.class);
          PendingIntent pendingIntent = PendingIntent.getService(AlarmService.this, 0, syncIntent, 0);
  
          AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
  
          Calendar calendar = Calendar.getInstance();
          calendar.setTimeInMillis(System.currentTimeMillis());
          calendar.add(Calendar.HOUR_OF_DAY, 9);
          alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 1000 * 60 * 60 * 24, pendingIntent);
  </code></pre>
  
  <p>What should i do to schedule an alarm to stop the service at 18h ?</p>
  
  <p>Thanks</p>