Alarmmanager does not run on configured interval

Asked

Viewed 82 times

2

In a method that records a AlarmManager to run every 30 seconds BroadcastReceiver:

public void play(View view) {

        Log.i("lgg", "Botão: broadPlay");

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 10);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        long intervalo = 30 * 1000;

        Intent tarefaIntent = new Intent(this, MyBroadcastReceiver.class);
        PendingIntent tarefaPendingIntent = PendingIntent.getBroadcast(this, 0, tarefaIntent, 0);

        AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                intervalo, tarefaPendingIntent);
    }

Broadcastreceiver:

public class Mybroadcastreceiver extends Broadcastreceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Log.i("lgg", "onReceive " + new Date());
}

But accompanying the Log, I see what time it performs at the correct interval, no time, hour takes 1min or even more, etc.

I wonder why it happens ?

And what method do the big apps (e.g. Whatsapp) use for real-time checking ?

1 answer

1


From API 19 all repeating alarms, such as setRepeating(), will be inaccurate, is the same as using setInexactRepeating().

One solution is to use one of the repeating alarms, such as setExact() and schedule it again for the next time.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.