2016-06-29 8 views
0

J'ai une alarme répétée en haut de chaque heure pour vérifier si une notification doit être envoyée. J'ai des boutons on/off, mais le bouton ne semble pas l'annuler. Il ya quelques questions sur l'annulation des alarmes sur SO, mais aucun d'entre eux ont été en mesure d'aider. Voici ce que j'ai.Comment annuler une alarme répétée

Activité principale

public void onSendNotificationsButtonClick(View view) { 
    NotificationEventReceiver.setupAlarm(getApplicationContext()); 
} 

public void stopSendNotificationsButtonClick(View view) { 
    NotificationEventReceiver.cancelAlarm(getApplicationContext()); 
} 

récepteur d'événements s'étend WakefulBroadcastReceiver

public static void setupAlarm(Context context) { 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    PendingIntent alarmIntent = getStartPendingIntent(context); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
      getTriggerAt(new Date()), 
      NOTIFICATIONS_INTERVAL_IN_HOURS * AlarmManager.INTERVAL_HOUR, 
      alarmIntent); 
} 

public static void cancelAlarm(Context context) { 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    PendingIntent alarmIntent = getDeleteIntent(context); 
    alarmIntent.cancel(); 
    alarmManager.cancel(alarmIntent); 
} 

private static long getTriggerAt(Date now) { 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTime(now); 
    calendar.add(Calendar.HOUR, NOTIFICATIONS_INTERVAL_IN_HOURS); //change here to set notification interval 
    calendar.set(Calendar.MINUTE, 0); 
    calendar.set(Calendar.SECOND, 0); 
    return calendar.getTimeInMillis(); 
} 

private static PendingIntent getStartPendingIntent(Context context) { 
    Intent intent = new Intent(context, NotificationEventReceiver.class); 
    intent.setAction(ACTION_START_NOTIFICATION_SERVICE); 
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
} 

public static PendingIntent getDeleteIntent(Context context) { 
    Intent intent = new Intent(context, AlarmManager.class); 
    intent.setAction(ACTION_DELETE_NOTIFICATION); 
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
} 

@Override 
public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 
    Intent serviceIntent = null; 
    if (ACTION_START_NOTIFICATION_SERVICE.equals(action)) { 
     Log.i(getClass().getSimpleName(), "onReceive from alarm, starting notification service"); 
     serviceIntent = NotificationIntentService.createIntentStartNotificationService(context); 
    } else if (ACTION_DELETE_NOTIFICATION.equals(action)) { 
     Log.i(getClass().getSimpleName(), "onReceive delete notification action, starting notification service to handle delete"); 
     serviceIntent = NotificationIntentService.createIntentDeleteNotification(context); 
    } 

    if (serviceIntent != null) { 
     // Start the service, keeping the device awake while it is launching. 
     startWakefulService(context, serviceIntent); 
    } 
} 

modifier: La fonction a changé en fonction des réponses, ne fonctionne toujours pas annuler l'alarme.

public static void cancelAlarm(Context context) { 
    Intent intent = new Intent(context, NotificationServiceStarterReceiver.class); 
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0); 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    alarmManager.cancel(sender); 
} 
+0

double possible de [Comment annuler cette alarme à répétition?] (Http://stackoverflow.com/questions/3330522/how-to-cancel-this-repeating-alarm) –

Répondre

0

Il devrait être:

public static void cancelAlarm(Context context) { 
Intent intent = new Intent(this, AlarmReceive.class); 
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0); 
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
alarmManager.cancel(sender); 
} 

Cet article: How to cancel this repeating alarm?

+0

J'ai vu cet article et cela ne fonctionne pas, voir mon édition. – Drakorex

0

définir le code de requête unique intention dans l'attente lors de la configuration de l'alarme et l'annuler avec la même code de requête et d'annuler l'intention dans l'attente aussi.

public static void cancelAlarm(Context context) { 
    Intent intent = new Intent(this, AlarmReceive.class); 
    PendingIntent sender = PendingIntent.getBroadcast(this, request_code, intent, 0); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    alarmManager.cancel(sender); 
    sender.cancel(); } 
0
Please try this 

public void cancelAlarmIfExists(Context mContext, int requestCode, Intent intent) { 
    try { 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, requestCode, intent, 0); 
     AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); 
     am.cancel(pendingIntent); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
}