ilpadrino
ilpadrino

Reputation: 13

Handler and postDelayed doesn't work if milliseconds number is too high

I am trying to execute a service in the background. This service is started on boot and its function is to execute something every 8 hours. In this case it sends a file by mail using JavaMail API and gmail account. This is my code:

private Handler mHandler;
private Runnable updateRunnable = new Runnable() {
    @Override public void run() {
        new MailSenderActivity.MailSender().execute();


        queueRunnable();
    }
};

private void queueRunnable() {


    mHandler.postDelayed(updateRunnable, 28800000); 
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {

    mHandler = new Handler();
    queueRunnable();       
}

@Override
public void onDestroy() {
    //code to execute when the service is shutting down
}
@Override
public void onStart (Intent intent, int startid) {
    //code to execute when the service is starting up
}

If xxxx in mHandler,postDelayed(updateRunnable, xxxx) is 600000 (10 minutes in milliseconds), it runs perfectly, but it doesn't if it is 28800000 (8 hours in milliseconds)

Any suggestions?

Thanks in advance

Upvotes: 0

Views: 1208

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007286

I am trying to execute a service in the background. This service is started on boot and its function is to execute something every 8 hours.

Please don't do that. Please use AlarmManager to start an IntentService every 8 hours. That way, your service does not have to stay around in memory all that time.

If "xxxx" in "mHandler,postDelayed(updateRunnable, xxxx)" is 600000 (10 minutes in milliseconds), it runs perfectly, but it doesn't if it is 28800000 (8 hours in milliseconds)

Quite possibly, Android terminated your process. Again, this is not a problem if you use AlarmManager and an IntentService.

Upvotes: 3

Related Questions