Search code examples
androidoopalarmmanager

Execute alaramManager.setRepeating() on condition


I have implemented a simple app that shows notification at particular intervals using alaramManager just as shown here .

I have a static bool shouldNotify class level variable in main activity. I need setRepearing to execute only if my shouldNotify is true. How shall it be done?

EDIT:

One way is by notifying on condition(as specified in the first answer), but that will drain the battery as alaram will always be fired. Is there any better option?

Let me know if its too vague to be asked here. I am new to OOP.


Solution

  • Let your alarm be repeated as it is. Just put your if(shouldNotify) check in onReceive() method. Thus you will check, whether to send notification or not.

    EDIT

    I guess, it is not an optimal solution, yes. As an alternative, if you care about battery life, you can cancel alarm, when shouldNotifychanges to false and start again when it turns to true.