Sampada
Sampada

Reputation: 129

AlarmManager working for only a short span of time set

I have used an AlarmManager as:

 final Intent myIntent = new Intent(this.context, AlarmReceiver.class);
 alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
 final Calendar calendar = Calendar.getInstance();
 add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
 calendar.set(Calendar.HOUR_OF_DAY, tp1.getCurrentHour());
            calendar.set(Calendar.MINUTE, tp1.getCurrentMinute());


            myIntent.putExtra("extra", "yes");
           myIntent.putExtra("quote id", String.valueOf(quote));
            pending_intent = PendingIntent.getBroadcast(Addevent.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending_intent); 
}
});

This works fine if the time is set near to few hrs but if the span is greater like 9hrs , it the onReceive() method of the AlarmReceiver is immediately called, when the alarm is created.Where am I going wrong ?

Upvotes: 0

Views: 66

Answers (2)

G. Blake Meike
G. Blake Meike

Reputation: 6715

You have to use the kind of time that corresponds to the flags you use in the call to set. The two ELAPSED_REALTIME_... flags correspond to time since boot (elapsedRealTime). The two RTC_... flags correspond to time since the epoch (currentTimeMillis).

If you are scheduling a repeating even, be aware that the AlarmManager will not schedule tasks in the past. Since scheduling requires communication with a different process, it may, occasionally, take more than a millisecond to schedule. If you schedule the first occurrence less than a few ms in the future, it may just disappear.

Edited to add:

Finally, not clear how your code is supposed to work (what is in tp1) but you are using the Calendar method set, not add. set simply sets the value of the named field. It does not "carry" to the next field.

Upvotes: 1

Prashant
Prashant

Reputation: 187

Please find code snippet which I have used to schedule Alarm manager to trigger the event on next day.

 public void scheduleAlarm(..) {

            // time at which alarm will be scheduled here alarm is scheduled at 1 day from current time, 
            // we fetch  the current time in milliseconds and added 1 day time
            // i.e. 24*60*60*1000= 86,400,000   milliseconds in a day       
            Long time = new GregorianCalendar().getTimeInMillis()+24*60*60*1000;

            // create an Intent and set the class which will execute when Alarm triggers, here we have
            // given AlarmReciever in the Intent, the onRecieve() method of this class will execute when
            // alarm triggers.
            Intent intentAlarm = new Intent(this, AlarmReciever.class);

            // create the object
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

            //set the alarm for particular time
            alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1,  intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));

            Toast.makeText(this, "Alarm Scheduled for Tommrrow", Toast.LENGTH_LONG).show();

    }

Hope this helps...

Upvotes: 0

Related Questions