Skip to content Skip to sidebar Skip to footer

Setting Multiple Alarms On My Alarm Application

In my application I am able to play alarm tone on specified time using timepicker..But when i press the set alarm button again it replaces the previous alarm.. Could anyone help to

Solution 1:

This is because you always use the same Intent. If you want to have more than one alarm scheduled, you need to modify your intent. For instance you can define an integer id and increase it for every new alarm by one. Then you can use the code below to assign this id to the intent.

publicclassAlarmReceiverextendsActivity {

    privateintid=0;

    @OverridepublicvoidonClick(View v) {

        Intent intent=newIntent(SetAlarm.this,AlarmReceiver.class);
        intent.setData(Uri.parse("alarm:" + (id++)));

        ... rest of your code here
    }

}

If you want to cancel() alarm, you need to use intent with exactly this id.

Solution 2:

You are settings the same PendingIntent for both alarms according to documentation it will be replaced:

If there is already an alarm for this Intent scheduled (with the equality of two intents being defined by filterEquals(Intent)), then it will be removed and replaced by this one.

According to the documentation two intents are equals base on filterEquals(Intent) which mean:

Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.

So basically if you want to schedule two alarms just make sure that your Intents are distinct.

Solution 3:

you can use setRepeating like this

alm.setRepeating(type, triggerAtMillis, intervalMillis, operation);

Post a Comment for "Setting Multiple Alarms On My Alarm Application"