Thursday, December 12, 2013

Android: Adding and removing an alarm event (to be repeated each day)

Following code shows how to add an alarm for a given time along with setting a number as extra to identify which alarm was triggered.


ProfileSettings.java is a class containing following fields with getter setter.
Class ProfileSettings {
    private long _time;
    private int _type;
    private int _id;
}


Code to add the alarm is as follows:
public void addAlarmEvent(ProfileSettings setting) {
    Context context = Main.getInstance().getContext();
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
    intent.putExtra(AlarmBroadcastReceiver.PROFILE_CODE, setting.getProfileCode());
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, setting.getEventId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
    Long time = setting.getTime();
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, AlarmManager.INTERVAL_DAY, pendingIntent);
    Log.d(getApplicationName(), "addAlarmEvent(): Added alarm for id:" + setting.getEventId() + " Time:" + new Date(setting.getTime()).toString() + " Type:" + setting.getProfileCode());
}
AlarmBroadcastReceiver.java will receive the alarm event.
intent.putExtra will add a code which will be received in the intent, once the alarm triggers.
Passing setting.getEventId() to PendingIntent.getBroadcast will make help us in cancelling the specific alarm later.
FLAG_UPDATE_CURRENT tells to update the same Intent, if is already present.


Following is the method for cancelling the previously added alarm.
public void cancelAlarm(int id) {
    Context context = Main.getInstance().getContext();
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.cancel(pendingIntent);
    Log.d(getApplicationName(), "cancelAlarm(): Cancelled alarm for id: " + id);
}

"id" passed to cancelAlarm is same as setting.getEventId() used for adding the Alarm.
Please note that the context used to add the alarm must be the same for cancelling the alarm.


AlarmBroadcastReceiver.java shall receive the events triggered by AlarmManager. The code for this class is given below.
package com.example.receivers;
public class AlarmBroadcastReceiver extends BroadcastReceiver {

    public static final String PROFILE_CODE = "profile";

    public AlarmBroadcastReceiver() {}

    @Override
    public void onReceive(Context context, Intent intent) {
        int code = intent.getExtras().getInt(PROFILE_CODE, -1);
        switch (code) {
            case 1:
                Log.d(HelperUtils.getInstance().getApplicationName(), "OnReceive(): CODE_NORMAL");
                break;
            default:
                Log.d(HelperUtils.getInstance().getApplicationName(), "OnReceive(): Invalid code received");
                break;
        }
    }
}


Finally the AndroidManifest.xml entry:
<application ...>
    <receiver android:name="com.example.receivers.AlarmBroadcastReceiver">
    </receiver>
</application>

No comments:

Post a Comment

Android aar deployment in Maven - 2022

Introduction If you are working on android library project, you might be wondering how to publish it on Maven like this . Earl...