3

Ok, let me modify the question and make it much easier. I Hope you'll be able to get me the solution for this. Both the alarms should be scheduled simultaneously, which is not happening here. I am even using unique requestcode for the pending intent. HELP ME please.....

//On click Listener

private OnClickListener mOneShotListener = new OnClickListener() {
    public void onClick(View v) {

        Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
        PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,0, intent, 0);
        //I even tried sender = PendingIntent.getBroadcast(AlarmController.this,0, intent, PendinIntent.FLAG_UPDATE_CURRENT);


        //the alarm to go off 30 seconds from now.

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 30);

        // Schedule the alarm!

        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

        //adds 2 minutes to the time

        calendar.add(Calendar.MINUTE, 2);

        sender = PendingIntent.getBroadcast(AlarmController.this,1, intent,0);
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender2);
    }
};

OLD Question:
I want to set two alarms at the same time for two different operations to be performed.

For Eg. A user sets alarm at 2'O clock and sets the duration to 15 mins. The first alarm should fire at 2'O clock which performs function1 and the second alarm should fire at 2:15 as the user specified the duration as 15 mins which performs the function2. This operation should be repeated everyday at 2'O clock unless the user changes the time.

I am calling this both functions on button click:

On Click Event

saveButton.setOnClickListener(new View.OnClickListener() {  
    public void onClick(View view) {  
        ..........  
        ..........  
        new ReminderManager(this).setReminder(mRowId, mCalendar);  
        new ReminderManager(this).wakeReminder(mRowId, mCalendar, duration);  
        }  
    }

The setReminder contains

//sets alarm at 2'O clock

public void setReminder(Long taskId, Calendar when, String duration){  
    Intent i = new Intent(mContext, OnAlarmReceiver.class);
    i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId);    
    PendingIntent pi = PendingIntent.getBroadcast(mContext,(int)System.currentTimeMillis(), i, PendingIntent.FLAG_UPDATE_CURRENT);    
    mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);    
}

The wakeReminder contains

//adds duration i.e. 15mins

public void wakeReminder(Long taskId, Calendar when, String duration){
    Intent i = new Intent(mContext, OnAlarmReceiverWake.class);
    i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId);
        Long d = Long.parseLong(duration);
        Long mins = d*60*100; 
        Long milli = when.getTimeInMillis() + mins;
        PendingIntent  pi = PendingIntent.getBroadcast(mContext, (int)System.currentTimeMillis(), i, PendingIntent.FLAG_UPDATE_CURRENT);
        mAlarmManager.set(AlarmManager.RTC_WAKEUP, milli, pi);
}

I have noticed that whenever mAlarmManager.set(); is executed successfully the LogCat shows notification like
"enqueueToast pkg=com.jellboi.android.togglemode callback=android.app ITransientNotification$stub$proxy@43c0c5f8 duration=0"

but when I set both the alarms simultaneously the notification is not shown when the mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi); is executed but the notification is shown when mAlarmManager.set(AlarmManager.RTC_WAKEUP, milli, pi); is executed. Also the notification is set for the original time i.e. 2'O clock and not after adding 15mins to it's duration.

Please help, I tried a lot of ways to call this functions at different places like after the 1st alarm is fired but all in vain.

Umar Maniar
  • 512
  • 1
  • 5
  • 14
  • 1
    It may help: http://stackoverflow.com/questions/6649402/alarm-manager-scheduling-multiple-non-repeating-events – inder Jul 14 '11 at 12:54

6 Answers6

10
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,0, intent, 0);

You have problem in this line. If you want to simultaneously fire two alarma then you should do something like this

PendingIntent sender =       PendingIntent.getBroadcast(AlarmController.this,giveUniqueRequestIdsHere, intent, 0);
Prativa
  • 374
  • 6
  • 25
4
sender = PendingIntent.getBroadcast(this, (int) System.currentTimeMillis(), i, PendingIntent.FLAG_ONE_SHOT);

Do the same for your PendingIntent.getActivity();

swathi
  • 663
  • 1
  • 5
  • 7
2

Hi try using this to generate request I was generating my own request code but this line worked for me

final int intent_id = (int) System.currentTimeMillis();
Vishal Pawar
  • 3,843
  • 3
  • 24
  • 53
0

When launch new OneShotAlarm.class, it have to design launch mode. Default setting would ignore the second time launch, if the first instance still existed.

Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
Kislingk
  • 1,199
  • 12
  • 20
0

seems like you forgot to set "sender2" before your second call.

you change the value of sender, then put sender2 as the argument.

-1

Try this:

Calendar cal = new GregorianCalendar();
Date date = cal.getTime();
cal.clear();

Cursor c = database.query(mArray.get(mPosition), new String[]{"days"},"days"+"!="+0, null, null, null, null);
cal.set(Calendar.YEAR,2011);
cal.set(Calendar.MONTH,date.getMonth());
cal.set(Calendar.DAY_OF_MONTH,date.getDate());
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(nextScreen.this, AlaramReciver.class);
i.putExtra("Start",mArray.get(mPosition));

Editor medit = getSharedPreferences("Start", 0).edit();
medit.putString("Start", mArray.get(mPosition));medit.commit();

PendingIntent pendingintent = PendingIntent.getBroadcast(this,mPosition, i, 0);
mgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingintent);

You have set to the time like this, and once you set time in alarm manager it will automatically call the functions daily. Use different request code for pendingintent.

If you want call second function. You just add the time in 15 min in the

cal.set(Calender.MINUTE,min+15);
Yi Jiang
  • 46,385
  • 16
  • 133
  • 131
kannappan
  • 2,218
  • 2
  • 24
  • 35
  • Hi,Yi Jiang thanks for the reply,I am setting my time correctly an the alarm fire perfectly at the specified time as I specified in my code.I've just omitted my code where I am specifying the time. The problem which I am not able to solve is, I am setting the two alarms simultaneously,eg.first at 2'O clock and other at 2:15 at the same time as soon as I click the save button. Now only 1 alarm is fired, i.e. the first one at 2'O clock, the other one doesn't nor it shows any error.When I debugged,the logCat shows dat only 1 alarm is queued and not the other one & m using currentSystemTime for pi – Umar Maniar Apr 06 '11 at 06:42