16

I need to check data update periodly, but the data is only updating during the daytime, so I want this repeating action run only in that time section for saving battery and bandwidth.

What should I do?

virsir
  • 14,359
  • 23
  • 71
  • 108

2 Answers2

29

If the service is talking to the cloud with HTTP get/post/whatever requests, then note that a C2DM solution would net better battery life, and that a SyncAdapter solution could provide a few benefits. (I recommend watching the Google I/O videos on both topics.)

The following code does something close to what you originally asked about.

public class MyUpdateService extends IntentService
{
  public MyUpdateService()
  {
    super(MyUpdateService.class.getSimpleName());
  }

  @Override
  protected void onHandleIntent(Intent intent)
  {
    // Do useful things.

    // After doing useful things...
    scheduleNextUpdate();
  }

  private void scheduleNextUpdate()
  {
    Intent intent = new Intent(this, this.getClass());
    PendingIntent pendingIntent =
        PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // The update frequency should often be user configurable.  This is not.

    long currentTimeMillis = System.currentTimeMillis();
    long nextUpdateTimeMillis = currentTimeMillis + 15 * DateUtils.MINUTE_IN_MILLIS;
    Time nextUpdateTime = new Time();
    nextUpdateTime.set(nextUpdateTimeMillis);

    if (nextUpdateTime.hour < 8 || nextUpdateTime.hour >= 18)
    {
      nextUpdateTime.hour = 8;
      nextUpdateTime.minute = 0;
      nextUpdateTime.second = 0;
      nextUpdateTimeMillis = nextUpdateTime.toMillis(false) + DateUtils.DAY_IN_MILLIS;
    }
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
  }
}
Programmer Bruce
  • 58,151
  • 6
  • 96
  • 93
  • I think this just runs once, you might want to use alarmManager.setRepeating to run it every 15 mins, correct me if I am wrong. – Sana Nov 14 '11 at 19:49
  • 1
    No, it is not only executed once as it schedules an alarm. When this alarm is triggered (15 minutes later), the service runs for a second time and so on. – caw Jan 28 '12 at 15:55
  • Why would you do this instead of using setRepeating? This seems less reliable, and like more work. – Keith Jul 19 '13 at 18:56
  • 2
    people have created setRepeating for a purpose. find it. then use it. technically the example works too. – tony9099 Sep 18 '13 at 19:14
  • 1
    If you are using Joda time (http://www.joda.org/joda-time/), the time computations can be highly simplified in this code. For instance the 3 lines to add 15 minutes to the current time can be rewritten like that : `DateTime nextUpdateTime = DateTime.now().plusMinutes(15);` – Pierre-Antoine Nov 17 '13 at 16:02
  • What is the difference in calling `scheduleNextUpdate()` in `onCreate` on `onHandleIntent` ? @programmer-bruce – HoseinIT Nov 26 '17 at 06:49
0

Follow these easy steps to keep servce alive forever in android device. 1. Call a service by using alarm manager for every 15 minutes. 2. return START_STICKY in onStart method. 3. In on destroy call the alarm manager and restart service by using startService method. 4.(Optional)Repeat the point number 3 in onTaskRemoved method.

Syed Danish Haider
  • 1,126
  • 9
  • 14