5

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 notifications- but they aren't, so I decided -following posts such as Delete alarm from AlarmManager using cancel() - Android to make the AlarmManager a static variable so the same instance can be reached from the whole app. The way I'm doing this is having the following method in my main class:

public static AlarmManager getAlarmManagerInstance() {
        if (sAlarmManager == null && sContext != null)
            sAlarmManager = (AlarmManager) sContext
                    .getSystemService(Context.ALARM_SERVICE);
        return sAlarmManager;
    }

and in the the sContext variable will be instantiated this way:

@Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_main);
        sContext = this;
        initActionBar();
    }

Is it a good idea to create a singleton pattern from this variable? Is there any better approach?

Thanks a lot in advance.

Community
  • 1
  • 1
noloman
  • 11,411
  • 20
  • 82
  • 129
  • use the same id to create pending intent.. that will do the same job – Pankaj Kumar Aug 13 '13 at 08:25
  • but I'm using the ids of the tasks I want to create/modify in order to create the pending intent, in such a way as `PendingIntent pendingFirstAlarmIntent = PendingIntent.getBroadcast( this, mGoal.getId().intValue(), intent, PendingIntent.FLAG_ONE_SHOT);`.. is it still the same? – noloman Aug 13 '13 at 08:27
  • Yes this is same... If this is not cancelling the alarm, try to use different flags... – Pankaj Kumar Aug 13 '13 at 08:34
  • Show the code you are trying to do this with. – Squonk Aug 13 '13 at 08:34
  • @Squonk question updated, thanks a lot – noloman Aug 13 '13 at 08:40

2 Answers2

1

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.

JanBo
  • 2,925
  • 3
  • 23
  • 32
  • but I already have a `BroadcastReceiver` with the `ACTION_BOOT_COMPLETED` permission so it can recreate the alarms from a DB when rebooting the phone. I just don't understand the thing of using the IDs in the `PendingIntent`, since I thought that parameter we pass to the `PendingIntent` is supposed to be the ID of the tasks, not the alarm. – noloman Aug 13 '13 at 11:32
  • If you dont put the ID, then when you create a PendingIntent with an Intent starting the same activity/service ( different extra values dont count ) Android wont be able to make a difference between them and since you pack them in Alarms when you schedule them, the end result will be that every time you create a new alarm the old one is canceled automatically without you knowing. And since you said your AlarmManagers are connected with your notifications then you should use different IDs, maybe use the same ID as for the notification if you gave them unique IDs – JanBo Aug 13 '13 at 11:44
  • http://stackoverflow.com/questions/7496603/how-to-create-different-pendingintent-so-filterequals-return-false ...also consider setting an action for your intent, and you can filter them that way if you dont like setting an id for the PendingIntent. – JanBo Aug 13 '13 at 11:48
  • I'm not really sure I'm following you but, I'm using as IDs for the notifications the task ID, as well as for the pending intents that create those alarm.. so now, should I also create different AlarmManagers? one for each alarm I will create, with the same ID as the task or what? getting lost now – noloman Aug 13 '13 at 13:32
  • I got lost too now :) . Like the answer above said, you dont actually create alarms, you schedule them like a service. You should schedule multiple alarms if you need your work to be done at different schedules and/or doing different operations. – JanBo Aug 13 '13 at 15:03
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)

Manolo Garcia
  • 3,785
  • 2
  • 19
  • 19