Skip to content Skip to sidebar Skip to footer

Static Alarmmanager In Android

I'm developing a simple tasks app in Android and I need to create notifications via an AlarmManager. My problem is that I have certain alarms that should be deleted -and thus their

Solution 1:

Android documentation says:

You do not instantiate this class directly; instead, retrieve it through Context.getSystemService(Context.ALARM_SERVICE).

AlarmManager is just a class that provides access to the system alarm services.

This services are running in the system so don't care about them just use AlarmManager as an interface to interact with them.

So each time that you need to access to this service just retrieve it as the documentation says:

Context.getSystemService(Context.ALARM_SERVICE)

Solution 2:

I would advise against creating a static Alarm.

You should follow the advice given in the comments, to use IDs given to your PendingIntents, this way you can cancel/update your alarms surely from any place within your application.

Reason why i advised against static Alarm:

The following scenario can occur, you schedule the alarm and make a static reference to it, then the user reboots the phone. Your alarm is gone and so is the static reference to it.

If you need your alarms to work in such scenario, you should write their ids and required info in shared preferences/database/file and reschedule them onBoot or on some other event suitable for your app.

Post a Comment for "Static Alarmmanager In Android"