I set alarms in my application using AlarmManager from several actions.
To avoid redundant code, I created the following class:
public class CustomAlarmManager { private static final String SHARED_PREF_REQUEST_CODE = "requestCode"; private static final String KEY_REQUEST_CODE = "kRequestCode"; private CustomAlarmManager() { } public static void setNewAlarm(Context context, long timeInMillis) { Intent intent = new Intent(SomeOtherClass.ALARM_ACTION); intent.putExtra(SomeOtherClass.KEY_ALARM_TIME, timeInMillis); PendingIntent pendingIntent = PendingIntent.getBroadcast( context.getApplicationContext(), getNewCode(context), intent, PendingIntent.FLAG_ONE_SHOT); AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE); if (Build.VERSION.SDK_INT >= 23) { am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent); } else if (Build.VERSION.SDK_INT >= 19) { am.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent); } else { am.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent); } }
Therefore, when I would like to set an alarm, I can simply call the following from anywhere in my application:
CustomAlarmManager.setNewAlarm(aContext, someTimeInMillis);
My question is:
Should I worry about Context leak here?
I did not keep links to it, so I think I'm good, but I'm not sure.
Is this a good approach?
source share