20

I have seen a lot of tutorials and been trying for 2 hours now , though something is still wrong. I am very nervous now :) I want to set an alarm e.g. to 16:25 to go off, but nothing happens. I have this code:

   Calendar cur_cal = new GregorianCalendar();
    cur_cal.setTimeInMillis(System.currentTimeMillis());

    Calendar cal = new GregorianCalendar();
    cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
    cal.set(Calendar.HOUR_OF_DAY, 16);
    cal.set(Calendar.MINUTE, 25);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

I have also tried this:

 cal.set(Calendar.AM_PM, cur_cal.get(Calendar.AM_PM));
    cal.set(Calendar.HOUR, 4);

My final goal is to make it a repeating alarm, e.g. it should go off every day at the set time.

Update 01.17.2011. Still not working. I have this code:

           Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, 2011);
            cal.set(Calendar.MONTH, Calendar.JANUARY); 
            cal.set(Calendar.DAY_OF_MONTH, 17);
            cal.set(Calendar.HOUR_OF_DAY, 16);
            cal.set(Calendar.MINUTE, 58);
         cal.set(Calendar.SECOND, 0);
          cal.set(Calendar.MILLISECOND, 0);

I have also tried this:

 cal.set(Calendar.HOUR, 4);
 cal.set(Calendar.AM_PM, Calendar.PM);

and this:

cal.set(Calendar.HOUR_OF_DAY, 4 );
            cal.set(Calendar.AM_PM, Calendar.PM);
erdomester
  • 11,491
  • 31
  • 126
  • 226

3 Answers3

14
package your.pack.name;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class AlarmActivity extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Calendar cal = Calendar.getInstance();

        cal.setTimeInMillis(System.currentTimeMillis());
        cal.clear();
        cal.set(2012,2,8,18,16);

        AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
       // cal.add(Calendar.SECOND, 5);
        alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);


    }
}
Dipin
  • 1,065
  • 6
  • 18
  • 2
    i try this and i got error on this line: Intent intent = new Intent(this, AlarmReceiver.class); what can be the problem ? – Gold Aug 21 '12 at 18:31
  • @Gold He created a java .class with the name AlarmReceiver, and it's set to be the callback for alarms. You have to either create one of your own or create another one and change AlarmReceiver to whatever your Activity's name is. – tfrascaroli Sep 10 '13 at 08:09
  • 2
    sorry @chandukadam,... can you explain what does "2012,2,8,18,16" means? – gumuruh Aug 06 '14 at 06:04
  • @chandu kadam :: can i set alarm between two specific dates at particular day see below link [http://stackoverflow.com/questions/26667762/set-alarm-notification-between-two-specific-date-with-particular-day-in-android] – Harshal Kalavadiya Oct 31 '14 at 05:41
  • why cal.setTimeInMillis(System.currentTimeMillis());? – yeahman Feb 26 '16 at 19:37
  • @ChandrakantKadam what will AlarmReceiver.class will contain. can you please explain – bhavikshah28 Dec 08 '17 at 08:40
  • is that 2012 is year, 2 is month, 8 is day, 18 is hour, and lastly 16 is minute am i right? – gumuruh Dec 25 '20 at 08:13
11

Usually you shouldn't obtain Calendar like you do, there is Calendar.getInstance() method for that:

Calendar cal = Calendar.getInstance();

That gives you a calendar with all fields set to current date, then just:

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

alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
Konstantin Burov
  • 67,168
  • 16
  • 112
  • 93
  • 2
    Thank you, its working now. But why doesnt it work if i set cal.set(Calendar.YEAR, 2011); cal.set(Calendar.MONTH, 1); cal.set(Calendar.DAY_OF_MONTH, 16); ? – erdomester Jan 16 '11 at 11:39
  • @erdomester Use cal.set(Calendar.MONTH, Calendar.JANUARY) .. '1' states for February I guess. – Konstantin Burov Jan 16 '11 at 11:48
  • 1
    I don't get how the answer supplied makes your code work. When I schedule an alarm it works perfectly for today, but the moment I add the YEAR and MONTH it stops working.... – Eugene van der Merwe Jun 15 '12 at 20:15
7

The following code works perfectly for alarm. The date and time i mentioned here is : 2012- June- 28, 11:20:00 AM. And the most important thing is, month is specified from 0 t0 11 only. Means June should be specified by 5.

        AlarmManager objAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);              
        Calendar objCalendar = Calendar.getInstance();
        objCalendar.set(Calendar.YEAR, 2012);
        //objCalendar.set(Calendar.YEAR, objCalendar.get(Calendar.YEAR));
        objCalendar.set(Calendar.MONTH, 5);
        objCalendar.set(Calendar.DAY_OF_MONTH, 28);
        objCalendar.set(Calendar.HOUR_OF_DAY, 11);
        objCalendar.set(Calendar.MINUTE, 20);
        objCalendar.set(Calendar.SECOND, 0);
        objCalendar.set(Calendar.MILLISECOND, 0);
        objCalendar.set(Calendar.AM_PM, Calendar.AM);         

        Intent alamShowIntent = new Intent(this,AlarmActivity.class);
        PendingIntent alarmPendingIntent = PendingIntent.getActivity(this, 0,alamShowIntent,0 );

        objAlarmManager.set(AlarmManager.RTC_WAKEUP,objCalendar.getTimeInMillis(), alarmPendingIntent);
Jomia
  • 3,416
  • 10
  • 44
  • 63