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.