3

I wan to set alarm with notification at specific date. Then I am using AmarmManager with NotificationManager currently. When I set selected date from dateDialog, the alarm is working. How can I put calendar value on alarm set with fixed time? I want to repeat alarm every day on fixed time such as at 9:00 in the morning. Currently, alarm is ignoring the time on specific date. Could you help me? Many thanks.

confirmButton.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) {
            //set alarm with expiration date                
            am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            setOneTimeAlarm();
            Toast.makeText(fridgeDetails.this, "Alarm automatic set", Toast.LENGTH_SHORT).show();
            setResult(RESULT_OK);
            finish();
        }
        public void setOneTimeAlarm() {
        c.set(Calendar.HOUR_OF_DAY, 10);
        c.set(Calendar.MINUTE, 0);
        c.set(expiredYear, expiredMonth, expiredDay);
        Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(fridgeDetails.this, 
                        0, myIntent, PendingIntent.FLAG_ONE_SHOT);
        am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);

        }
    });


private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() 
{
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        expiredYear = year;
        expiredMonth = monthOfYear;
        expiredDay = dayOfMonth;
        displayDate();
    }
};  

public class AlarmService extends BroadcastReceiver{
NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
    nm = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
          CharSequence from = "Check your fridge";
          CharSequence message = "It's time to eat!";
          PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(), 0);
          Notification notif = new Notification(R.drawable.ic_launcher,
            "Keep Fridge", System.currentTimeMillis());
          notif.setLatestEventInfo(context, from, message, contentIntent);
          nm.notify(1, notif);
         }  
}
wholee1
  • 1,491
  • 13
  • 34
  • 51

4 Answers4

10

If you want to play alarm for particular date of months suppose you want to play alarm on 11-june-2012 then you should specify like this but June is 6 month then you should specify 5 in Calender.Month.

Calendar cal=Calendar.getInstance();
cal.set(Calendar.MONTH,5);
cal.set(Calendar.YEAR,2012);
cal.set(Calendar.DAY_OF_MONTH,11);

cal.set(Calendar.HOUR_OF_DAY,16);
cal.set(Calendar.MINUTE,10);
cal.set(Calendar.SECOND,0);

Intent _myIntent = new Intent(getApplicationContext(), ReceiverClass.class);
PendingIntent _myPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 123, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);
AlarmManager _myAlarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
//_myAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), _myPendingIntent);
_myAlarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), _myPendingIntent);  
Sam
  • 86,580
  • 20
  • 181
  • 179
Jai
  • 101
  • 1
  • 3
  • 2
    Welcome to Stack Overflow! This is a great first answer for setting an Alarm, you provided a good description and code to back it up. But please proof your code a little more: you were setting the Calendar object three times and had a lot of unnecessary spacing. Also if the indexes of the month are confusing simply use this: `cal.set(Calendar.MONTH,Calendar.JUNE);` – Sam Jun 11 '12 at 19:01
3

Hey this is how to set alarm on android AlarmManager to spesific date (android alarmmanager set alarm on date) I have been searching all over for this. pay attention to the value of the month!!

Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
//cal.add(Calendar.SECOND, 10);

cal.set(Calendar.DATE,19);  //1-31
cal.set(Calendar.MONTH,Calendar.DECEMBER);  //first month is 0!!! January is zero!!!
cal.set(Calendar.YEAR,2012);//year...

cal.set(Calendar.HOUR_OF_DAY, 16);  //HOUR
cal.set(Calendar.MINUTE, 39);       //MIN
cal.set(Calendar.SECOND, 10);       //SEC


// Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(MainActivity.this, alarmAct.class);
PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0,intent, 0);

//or if you start an Activity
//PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,intent, 0);

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
Zeev G
  • 2,191
  • 21
  • 37
2

You should call public void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)(see here) to repeat the alarm. For example, you want to fire the alarm on 9:00 am every day, you can do :

Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 9);
c.set(Calendar.MINUTE, 0);
setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent);

Also,set the last parameter to 0 when initilazing the PendingIntent.

PendingIntent pendingIntent = PendingIntent.getBroadcast(fridgeDetails.this, 
                        0, myIntent, 0);
Huang
  • 4,812
  • 3
  • 21
  • 20
  • Thank you for your replying!! I am wondering about setRepeating. If I want to set repeating alarm after specific date, is it right way? for example, set alarm date is 01/12/2011. If I set repeating in the above coding, is it working alarm interval day after 01/12/2011, not before? just I am wondering setRepeating. Is it true? – wholee1 Dec 01 '11 at 14:46
  • The 2nd argument is the first time when the alarm should be fired. If you want to repeat the alarm after a specific date, you can set the calendar to that date, and set the calendar time as the 2nd parameter. The 3rd argument is the interval time between two alarms, so, it should be always AlarmManager.INTERVAL_DAY in your case. – Huang Dec 01 '11 at 14:50
0
cal_alarm.setTime(dat);
        cal_alarm.set(Calendar.MONTH,3);
        cal_alarm.set(Calendar.YEAR,2012);
        cal_alarm.set(Calendar.DAY_OF_MONTH,4);
        cal_alarm.set(Calendar.HOUR_OF_DAY,19);//set the alarm time
        cal_alarm.set(Calendar.MINUTE, 15);
        cal_alarm.set(Calendar.SECOND,0);


        if(cal_alarm.before(cal_now)){//if its in the past increment
            cal_alarm.add(Calendar.DATE,1);
        }

        Intent intent = new Intent(AlarmManagerTestActivity.this,
                AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            AlarmManagerTestActivity.this, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), pendingIntent);  
}

This may help you