android - alarm manager service starting every oncreate -


i using alarm manager inexactrepeating starting pendingintent.

works great - problem service running every time app started, after ondestroy (ie orientation change, manually swiping app in recent).

i have set 20 min intervals, , long dont ondestroy app - oncreate automatically starts service screws timing. service ever called, once, through alarm manager.

how check see if app needs run it? boolean of running inside service in shared preferences or something?

you place alarmmanager in class application class creates once during application startup. ensure app creates 1 instance of alarmmanager when first starts up.

in other words, create class extends application, , in oncreate(), create instance of alarmmanager class , store instance in application class. inside class holds alarmmanager, in constructor create new instance of alarmmanager. in application class, create getter method of app's other activities or fragments retrieve , use 1 instance of alarmmanager.

note there debate whether should use application class store global application state, opposed static singleton.


Comments