3

I have this method:

public static Runnable runAtDate(Date date, final TimerCallback callback)
{
    Date now = new Date();
    long timeTo = date.getTime() - now.getTime();

    if (timeTo <= 0)
    {
        if (callback != null)
            callback.onRun();
    }
    else
    {
        final Runnable runnable = new Runnable()
        {
            @Override
            public void run()
            {
                if (callback != null)
                    callback.onRun();
            }
        };

        handler.postDelayed(runnable, timeTo);
        return runnable;
    }

    return null;
}

This works fine when the date is around a few hours later (1, 2, 3) but around 8 hours this method does not work anymore. And since other timers have not been reliable (delayed and whatnot) I chose to use this, because it always execute right on time - when it works.

My question is, what is the maximum hours that a postDelay can run? Do I have to add a new postDelay every (some hours) adding the difference between (some hours) and the exact date?

Deukalion
  • 2,516
  • 9
  • 32
  • 50
  • 6
    use `postAtTime` instead, note that posted `Runnables` are forgotten when your app process is killed by the OS, why dont you want to use `AlarmManager`? – pskink Jan 18 '16 at 17:06
  • App is not killed after whatever MAX_HOUR is but it doesn't get executed eitherway. I'll look at postTime, haven't even thought about it. BUt, since the app is not killed - there seems to be some kind of time limit, which still would be nice to know. – Deukalion Jan 18 '16 at 19:28
  • there is no such limit, see the [sources](http://androidxref.com/6.0.1_r10/xref/frameworks/base/core/java/android/os/MessageQueue.java#533), why dont you want to use `AlarmManager`? – pskink Jan 18 '16 at 20:02
  • @pskink checking the sources of Looper, it seems that postDelayed just eventually calls sendMessageAtTime, which is exactly what postAtTime does, so there's no difference. http://androidxref.com/6.0.1_r10/xref/frameworks/base/core/java/android/os/Handler.java#565 – Greg Ennis Apr 03 '19 at 15:08
  • @GregEnnis no difference in what? – pskink Apr 04 '19 at 19:07
  • @Deukalion I am facing the same problem. Have you found a solution? – Dinh Quang Tuan Jul 03 '21 at 15:12

0 Answers0