2

I have started alarm of 9:00 AM in android on Boot Completed. But fires alarm every minute after it has completed boot.

My Requirement is it should set Alarm after boot but only fires alarm at 9:00 AM.

Here is my code: public class AlarmUtil { private PendingIntent alarmIntent;

public static void setAlarm(Context context) {

    AlarmManager alarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 9);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 00);
    calendar.set(Calendar.AM_PM, Calendar.AM);
    //calendar.setTimeInMillis(calendar.getTimeInMillis());
    calendar.add(Calendar.SECOND, 1);

    Intent intent = new Intent(context, Services.class);
    PendingIntent pintent = PendingIntent.getService(context, 123456789,
            intent, 0);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(), 1 * 60 * 1000, pintent);

}
}

public class AlarmBroadcastReciever extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        Toast.makeText(context, "Booted!", Toast.LENGTH_SHORT).show();
        AlarmUtil.setAlarm(context);

    }
}
}

services (My service Class)

public class Services extends IntentService {

public Services(String name) {
    super("services");
    // TODO Auto-generated constructor stub
}

public Services() {
    super("services");
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate() {
    super.onCreate();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    MyApp.counter += 1;

    Toast.makeText(getApplicationContext(),
            "Service started: " + MyApp.counter, Toast.LENGTH_LONG).show();
    return START_STICKY;
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
protected void onHandleIntent(Intent intent) {
    Toast.makeText(getApplicationContext(), "handling intent",
            Toast.LENGTH_LONG).show();

}

}

where i am lacking. Please help me. Thanks in advance.

Deepika Lalra
  • 1,035
  • 1
  • 10
  • 24
  • look at this document http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating%28int,%20long,%20long,%20android.app.PendingIntent%29 – RajaReddy PolamReddy Jun 19 '14 at 07:10

3 Answers3

5
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
        calendar.getTimeInMillis(), 1 * 60 * 1000, pintent);

By calling setRepeating(), you are instructing it to fire the intent on every minute. You probably meant to use set() or setExact() instead.

Additionally, you're setting the alarm for 9:00 on the current date. I'm not exactly sure what your objective is but if you want an alarm for the "next 9:00" (i.e. as an alarm clock) you should probably add a day if the current time is greater than 9:00. From the documentation:

If the stated trigger time is in the past, the alarm will be triggered immediately.

EDIT: If what you need is to fire this alarm every day at 9:00, then setRepeating() is correct, but the time in milliseconds should be AlarmManager.INTERVAL_DAY. Be mindful of the note about past alarms, though. If you boot your phone, say, at 10:00 AM, and you use your current code, you will get an immediate alarm in addition to the future ones.

And, as @DerGolem pointed out, if you target API level 19 then these alarms will be inexact (and there is no setRepeatingExact()). If you need exact alarms, then you will have to schedule one with setExact(), then the next one when that one fires, and so on.

matiash
  • 54,791
  • 16
  • 125
  • 154
  • i have just added i min gap just for testing purpose. the problem was if i reboot system at 8:56AM. setAlarm() will get called and then it will start service after every min. Lets say Boot UP time is 8:56 Am and i want to get alarm at 9:00AM. It will make alarm at 8:57 Am then 8:58Am then 8:59. – Deepika Lalra Jun 19 '14 at 07:20
  • @DeepikaLalra I'm not sure I understood. If you want a single alarm, use `set()`. `setRepeating()` will fire repeating alarms. Isn't that the problem you're currently having? – matiash Jun 19 '14 at 07:22
  • `Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.` – Phantômaxx Jun 19 '14 at 07:27
  • @matiash i have tried this. it works and fired at exact 9:00. but i want to ask will it fire this alarm everyday at 9:00am or not. because i want to be fired everyday 9:00AM. – Deepika Lalra Jun 19 '14 at 07:33
  • @DeepikaLalra edited answer with this new info (I'm sorry but it really didn't understand that from your question). – matiash Jun 19 '14 at 07:39
1

you need to replace "1 * 60 * 1000" with AlarmManager.INTERVAL_DAY to set it daily

1

Hi I have used following code and able to generate alarm at 9 Am each day

alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmService.class);

    PendingIntent pintent = PendingIntent.getService(this, 123456789,
            intent, 0);

    // Set the alarm to start at 8:30 a.m.
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 9);
    calendar.set(Calendar.MINUTE, 00);


    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(), 1000 * 60 * 1 * 60 * 24, pintent);
Avtar Guleria
  • 2,126
  • 3
  • 21
  • 33