What happens when I start an alarm twice?

梦想的初衷 提交于 2019-12-10 12:34:23

问题


I'm Jumping trough hoops (well, it's not that complicated ofcourse) to avoid starting an alarm twice. The basic code goes like this:

AlarmManager mgr=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(this, MyService.class);
PendingIntent pi=PendingIntent.getService(this, 0, i, 0);
mgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);

Would it matter if I would actually run this code everytime my app starts? I'm not seeing any ill effects when calling this about 10 times as an overkill-experiment, but I can't find any reference as to if this is coincidence or expected behavior.

If it is not especially expected, it feels 'wrong'. It might get me in to trouble later if the behavior of the AlarmManager changes.


回答1:


Since the cancel method for AlarmManager is fine with a 'similar' Intent to cancel the alarm, we can say that the platform recognizes the intent given the class name. Hence calling this repeatedly shouldn't be a problem since the platform will know that for such a pending intent an alarm already exists.

Here is a post that talks something similar.



来源:https://stackoverflow.com/questions/5707656/what-happens-when-i-start-an-alarm-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!