Naruto
Naruto

Reputation: 9634

Create alarm service in android for multiple times

in my task application, i want to create alarm service for multiple items. Here is the code i followed to create service. Lets assume, if there are more than 5 items present in list. then we will sent alarm service for 5 different times.

What if the user comes and edits the time?, how to update the system and make sure it triggers at new instance?. What if the user delete the task, how to quickly removes the registered alram for the deleted task? What about uninstalling the app?, how to clear all the entries?, what if the system re-boots?

This is the code i'm following to register the task in alram services

   Calendar Calendar_Object  = Calendar.getInstance();
                     Calendar_Object.set(Calendar.MONTH, 8);
                     Calendar_Object.set(Calendar.YEAR, 2012);
                     Calendar_Object.set(Calendar.DAY_OF_MONTH, 6);

                     Calendar_Object.set(Calendar.HOUR_OF_DAY, 14);
                     Calendar_Object.set(Calendar.MINUTE, 25);
                     Calendar_Object.set(Calendar.SECOND, 0);


       Step 2:

            We need to create an alarm which help us to invoke the BroadCastReceiver on our specified time.

                       // MyView is my current Activity, and AlarmReceiver is the BoradCastReceiver
                         Intent myIntent = new Intent(MyView.this, AlarmReceiver.class);

                        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                        MyView.this, 0, myIntent, 0);

                       AlarmManager alarmManager = (AlarmManager)                              getSystemService(ALARM_SERVICE);

                    /* 
                       The following sets the Alarm in the specific time by getting the long value of the alarm  date time which is in calendar object by calling the getTimeInMillis(). Since Alarm supports only long value , we're using this method.
                   */

                alarmManager.set(AlarmManager.RTC, Calendar_Object.getTimeInMillis(),
                        pendingIntent);

Upvotes: 0

Views: 323

Answers (1)

AndiM
AndiM

Reputation: 2188

Use this method to set Alarm :

public static void startAlarm(Context context, int alarm_code, String time) {
        String[] arrTime = time.split(":");
        intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra("CODE", alarm_code);
        PendingIntent mAlarmSender = PendingIntent.getBroadcast(context,
                alarm_code, intent, 0);
        Calendar cal_alarm = Calendar.getInstance();
        cal_alarm.setTimeZone(TimeZone.getDefault());

        cal_alarm.set(Calendar.HOUR_OF_DAY, Integer.parseInt(arrTime[0]));
        cal_alarm.set(Calendar.MINUTE, Integer.parseInt(arrTime[1]));



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


        am.setRepeating(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, mAlarmSender);
    }

Here,"int alarm_code" is your unique code for particular item. And for update alarm first cancel the existing alarm and set new alarm for new time.For that use this method :

public static void cancelAlarm(Context context, int alarm_code) {

        AlarmManager am = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);

        intent.putExtra("CODE", alarm_code);
        am.cancel(PendingIntent.getBroadcast(context, alarm_code, intent, 0));
    }

Here,"int alarm_code" will be your unique code which you have used to set alarm.So that alarm with particular alarm_id will be cancel.You can also delete alarm with this method.

Same way you can clear all the alarms passing alarm_code to the given function.

You have to register receiver for system reboot and in onReceive of register you have to set all alarms again.

Hope this will help you.

Upvotes: 1

Related Questions