Koss Koss - 1 year ago 70
Android Question

AlarmManager fires after different times on different API's

This is the first time I use

. This is a part of the service:

Intent alarm = new Intent(this.context,AlarmReceiver.class);
boolean alarmRunning =(PendingIntent.getBroadcast(this.context,0, alarm,PendingIntent.FLAG_NO_CREATE) != null);
if(alarmRunning == false){
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.context,0,alarm,0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),15000,pendingIntent);

I thought that 15000 in
means 15 seconds. On my S4 mini with API 19 is it right. But with my S7 with API 23 the alarm fires every minute. Do anyone know the issue for this?

Here is my Log:

08-15 10:54:15.949 11495-11522/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 10:55:15.619 11495-12452/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 10:56:15.619 11495-13407/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 10:57:15.629 11495-14288/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 10:58:15.629 11495-15193/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 11:00:00.039 11495-16907/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 11:00:15.599 11495-17149/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 11:01:15.619 11495-18085/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 11:02:15.659 11495-19040/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 11:03:58.049 11495-19601/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 11:06:11.419 11495-19878/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 11:11:11.349 11495-24547/com.example.entwicklung1.designtestapp D/BackgroundRunning::
08-15 11:16:11.449 11495-29123/com.example.entwicklung1.designtestapp D/BackgroundRunning::

As I see, it fires now every few minutes.

Answer Source

Since Alarm Manager wakes the device which drains the battery, Android has some new ways to fire the pending intent. On devices having API more than or equal to 19, the alarms are fired, together, if two or more alarms have similar time to start the pending intent, so that it does not have to wake the device every time for each alarms. So the new modification that you have to do is, to place this code:

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

After this line of code, place this:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),15000,pendingIntent);

} else {
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),15000,pendingIntent);

Here, if Android API is greater than KITKAT, setExact method is used, and if not, the alarm is setted normally like you did. Hope this works, all the best.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download