AlarmManager does not run on configured

2

In a method that registers a AlarmManager to run every 30 seconds the 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 following the Log , I see what time it runs at the correct interval, time not, time it takes 1min or even more, etc.

Would you like to know why this happens?

And what method do the large app (eg whatsapp) use for real-time scanning?

    
asked by anonymous 09.03.2018 / 19:51

1 answer

1

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

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

    
09.03.2018 / 21:06