I made a Timed notification via BroadcastReceiver & AlarmManager:

static public void setReminderNotification(Context c){
    AlarmManager alarmManager = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);

    Intent alarmIntent = new Intent(c, ReminderReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(c, 12460, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    iTime = 24000;
    if (iTime > 0) {
        alarmManager.cancel(pendingIntent);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + iTime, iTime, pendingIntent);
    }else if(iTime == 0){
        alarmManager.cancel(pendingIntent);
    }
}

I recognized, that the alarm doesn´t start every 24 seconds. it depends. i made a logging in the Receiver: (Format: H - m - s )

Post Nofification: 10 - 47 - 0
Post Nofification: 10 - 47 - 13
Post Nofification: 10 - 47 - 37
Post Nofification: 10 - 48 - 1
Post Nofification: 10 - 48 - 25
Post Nofification: 10 - 49 - 0
Post Nofification: 10 - 49 - 13
Post Nofification: 10 - 49 - 37
Post Nofification: 10 - 50 - 1
Post Nofification: 10 - 50 - 25
Post Nofification: 10 - 51 - 0
Post Nofification: 10 - 51 - 13
Post Nofification: 10 - 51 - 37
Post Nofification: 10 - 52 - 1
Post Nofification: 10 - 52 - 42
Post Nofification: 10 - 53 - 0
Post Nofification: 10 - 53 - 13

I wonder why the notification is sometimes shorter / longer. Any ideas how to fix this?

有帮助吗?

解决方案

Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.

Please see here http://developer.android.com/reference/android/app/AlarmManager.html

其他提示

The AlarmManager is not precise as far as have come to understand it.
In the new API level 19 of AlarmManager you have the

public void setExact (int type, long triggerAtMillis, PendingIntent operation)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top