Reputation: 3973
private void scheduleAlarms(Context context) {
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intentOnAlaramReceiver = new Intent(context, OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentOnAlaramReceiver, 0);
String listOfIntervalConnection = Utils.getStringFromProperties(context, Properties.SP_LIST_OF_ENABLE_INTERVAL_CONNECTIONS, Properties.ENABLE_AFTER);
long enableAfter = DateUtils.MINUTE_IN_MILLIS * Long.parseLong(listOfIntervalConnection);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + enableAfter, enableAfter, pendingIntent);
}
and i try to cancel alarm like
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intentOnAlaramReceiver = new Intent(context, OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentOnAlaramReceiver, 0);
alarmManager.cancel(pendingIntent);
but sometimne is canceled sometimes still working. Why and how? What can i do?
Upvotes: 0
Views: 210
Reputation: 499
Try to use PendingIntent.FLAG_UPDATE_CURRENT
instead of 0; Like this:
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentOnAlaramReceiver, PendingIntent.FLAG_UPDATE_CURRENT);
Upvotes: 0
Reputation: 110
You are using different instances of Intent in the PendingIntent when starting the service and while canceling it.
set the service intent as an instance variable and use it in both the starting and canceling of the service.
Upvotes: 1