1

I'm creating an reminder based application.When user creates reminder at any time it is working fine.But if he created two alarms at the same time(I'm not taking sec into consideration)let's say 9:30 am.Second alarm is firing correctly but first one is coming after one minute i.e 9:31.

This is how I'm creating alarm:alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),60000, contentIntent);

There I have given repeat time is 60000ms that is why it is coming after 1min.

But I want two alarms to be triggered at the same time.I've tried with remaining 3 methods of "AlarmManager" class but nothing works.Any suggestions to achieve this.

This is my create alarm method:

    public void createAlarm(Context context,int id,String title,String date1,String voiceMsgUrl, String description, String owner,String type, String repFrequency)
    {
        eventIntent=new Intent(context, MyReceiver.class);
        eventIntent.putExtra("notifyId", id+"");
        eventIntent.putExtra("title",title);
        eventIntent.putExtra("date", date1);
        eventIntent.putExtra("voiceMsgUrl", voiceMsgUrl);
        eventIntent.putExtra("description", description);
        eventIntent.putExtra("owner", owner);
        eventIntent.putExtra("repFrequency", repFrequency);

        contentIntent=PendingIntent.getBroadcast(context, id, eventIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        calendar=Calendar.getInstance();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{

            date=format.parse(date1);
            calendar.setTime(date);

        }catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(calendar.getTimeInMillis()>System.currentTimeMillis()){

            AppzoyDebug.e("creating alarm at time", calendar.getTimeInMillis()+"  lll");
        alarmManager=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);


   //alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),3000, contentIntent);


        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), contentIntent);

        }
    }

I'm passing values to that method like below

alarm.createAlarm(ReminderActivity.this, id, title.getText().toString(), dueDate.getText().toString(), url, description.getText().toString(), ownerNameString, "REM",repFrequency);
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
venky
  • 175
  • 1
  • 3
  • 13
  • Have you tried `setExact()`? – Daniel Nugent Apr 21 '15 at 04:49
  • Yes.I tried that's not firing the second alarm at all. – venky Apr 21 '15 at 04:51
  • 1
    Are you using a different `requestCode` for the two alarms? – Daniel Nugent Apr 21 '15 at 04:53
  • yes i'm using different request codes for both.This is how i'm creating pending intent "contentIntent=PendingIntent.getBroadcast(context, id, eventIntent, PendingIntent.FLAG_UPDATE_CURRENT);" – venky Apr 21 '15 at 04:54
  • @DanielNugent if the requestCode was the same (and the Intents similar enough), the alarm would only fire once. But it seems that both the alarms are fired, just not at the same time. – A J Apr 21 '15 at 04:57
  • Yeah, since API level 19 pretty much all alarms are inexact. I would think that using `setExact()` would be your best bet. Can you post your code that you tried with `setExact()`? – Daniel Nugent Apr 21 '15 at 05:00
  • Also, take a look at this post: http://stackoverflow.com/questions/24724859/alarmmanager-setexact-with-wakefulbroadcastreceiver-sometimes-not-exact/24776900#24776900 – Daniel Nugent Apr 21 '15 at 05:03
  • @DanielNugent this is how i tried with setExact() method. alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), contentIntent); – venky Apr 21 '15 at 05:03
  • Can you edit that into the question, and include both alarms, including the calendar setup and also `PendingIntent` and `Intent` setup? – Daniel Nugent Apr 21 '15 at 05:05
  • Please check i've updated my question – venky Apr 21 '15 at 05:26
  • Use one alarm, and have the receiver immediately fire the other intent. – Kevin Krumwiede Apr 21 '15 at 05:33
  • @KevinKrumwiede i didn't get that, can you please explain once. – venky Apr 21 '15 at 05:38
  • So the user chooses two alarms in the UI of your app with the same time, correct? Then this function gets called each time the user sets an alarm? – Daniel Nugent Apr 21 '15 at 05:44
  • Yes, when ever user enter the data and clicks "ok" this method will be called – venky Apr 21 '15 at 05:52
  • Have the receiver for your `eventIntent` fire your `contentIntent`, or vice versa. – Kevin Krumwiede Apr 21 '15 at 06:02
  • yes i have broadcast receiver which will call when ever the alarm fires up. – venky Apr 21 '15 at 06:10
  • So when you used `setExact()` the first alarm fired at exactly the right time, and the second one didn't fire at all? – Daniel Nugent Apr 21 '15 at 06:18
  • No the second one is firing and first one is being ignored means not fired at all.For ex let's say my two alarms are A and B.First i create A and next B.B is getting fired but not A. – venky Apr 21 '15 at 06:20
  • sorry @DanielNugent it's my mistake i'm not handling the second alarm.Kind of idiot i am. – venky Apr 27 '15 at 06:24
  • @venky haha, we all have those moments. So is it working for you now? – Daniel Nugent Apr 27 '15 at 06:29
  • yes it's working perfect,Thank you for your valuable time. – venky Apr 27 '15 at 07:05
  • @venky: Did you get a solution to this .. If so, could you post the answer please. – Manu May 22 '19 at 07:10

2 Answers2

1

Use 2 different Alarm manager. 1 alarm manager has set 1 alarm at a time. So if you want to set more than 1 alarm at the same time then set different Alarm Manager for each alarm..

  • This doesn't really make sense. Can you post code how you think this would work? – Daniel Nugent Apr 21 '15 at 05:10
  • Ya this work properly for each alarm has set different alarm manager. when 2 alarm is set same time.. this case that only option in think. –  Apr 21 '15 at 05:13
-1

Use AlarmManager.ELAPSED_REALTIME_WAKEUP in place of AlarmManager.RTC_WAKEUPand check it.

Piyush
  • 18,895
  • 5
  • 32
  • 63
ManiTeja
  • 1
  • 1
  • I don't think that would work. That just makes the alarm timing based on time since the last boot time of the device: http://stackoverflow.com/questions/5938213/android-alarmmanager-rtc-wakeup-vs-elapsed-realtime-wakeup – Daniel Nugent Apr 21 '15 at 05:47
  • Try with different Intent objects and different Request Code for same Alarm Manager Object – ManiTeja Apr 21 '15 at 06:42