singleton - static AlarmManager in Android -
i'm developing simple tasks app in android , need create notifications via alarmmanager. problem have alarms should deleted -and notifications- aren't, decided -following posts such delete alarm alarmmanager using cancel() - android make alarmmanager static variable same instance can reached whole app. way i'm doing having following method in main class:
public static alarmmanager getalarmmanagerinstance() { if (salarmmanager == null && scontext != null) salarmmanager = (alarmmanager) scontext .getsystemservice(context.alarm_service); return salarmmanager; }
and in the scontext
variable instantiated way:
@override protected void oncreate(bundle bundle) { super.oncreate(bundle); setcontentview(r.layout.activity_main); scontext = this; initactionbar(); }
is idea create singleton pattern variable? there better approach?
thanks lot in advance.
android documentation says:
you not instantiate class directly; instead, retrieve through context.getsystemservice(context.alarm_service).
alarmmanager class provides access system alarm services.
this services running in system don't care them use alarmmanager interface interact them.
so each time need access service retrieve documentation says:
context.getsystemservice(context.alarm_service)
Comments
Post a Comment