Search code examples
androidalarmmanagerandroid-pendingintent

pending intent / alarm not working second time


My code is like this, on button click i execute

  Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis()+(1000*5));

                Intent intent = new Intent(LogoFrontScreen.this,Doubletest.class);
                PendingIntent alarmIntent =    PendingIntent.getBroadcast(LogoFrontScreen.this,2,intent,PendingIntent.FLAG_UPDATE_CURRENT);

                alarmMgr.setExact(AlarmManager.RTC_WAKEUP,
                        calendar.getTimeInMillis(), alarmIntent);
                PendingIntent alarmIntent1 =    PendingIntent.getBroadcast(LogoFrontScreen.this,2,intent,PendingIntent.FLAG_UPDATE_CURRENT);

                alarmMgr.setExact(AlarmManager.RTC_WAKEUP,
                        calendar.getTimeInMillis()+(2000), alarmIntent1);

then on receiver there is a log but that log is called only once, why this is happening ?


Solution

  • In stead of

    alarmMgr.setExact
    

    Try

    alarmMgr.setInexactRepeating
    

    And for scheduling multiple alarm you need to use unique id every time creating

    PendingIntent alarmIntent =    PendingIntent.getBroadcast(LogoFrontScreen.this,2,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    

    The second argument of getBroadcast(i.e. 2 in your case) need to be different for every alarm.

    Hope it will solve your problem :)