3

In my app there is a Service that is intended to run constantly in background once started. This service is started from an Activity and this is the onStartCommand() method:

MyService:
....

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(Intent intent, int flags, int startId)
    mHandler.postDelayed(new myRunnable(), scheduledTime);
}

I want to know if having a very big scheduledTime(lets say a couple of days in milliseconds), the Handler will still execute the Runnable? Or should I better use the AlarmManager for this?

Thanks.

Onik
  • 19,396
  • 14
  • 68
  • 91
Sebastian Breit
  • 6,137
  • 1
  • 35
  • 53

1 Answers1

2

I want to know if having a very big scheduledTime variable (lets say it represents a couple of days), the handler will still execute the runnable?

Yes, if the service and the thread the Handler posts to will be alive by the time.

Or should I better use the AlarmManager for this?

Yes.

Onik
  • 19,396
  • 14
  • 68
  • 91
  • If it is still gonna work, why should I better use AlarmManager then? – Sebastian Breit Jan 24 '16 at 23:11
  • @Perroloco Please, see [does Alarm Manager persist even after reboot?](http://stackoverflow.com/questions/12034357/does-alarm-manager-persist-even-after-reboot), [Android AlarmManager after reboot](http://stackoverflow.com/questions/12512717/android-alarmmanager-after-reboot) and similar posts. – Onik Jan 24 '16 at 23:30