C'è qualche modo in Android per rilevare quando un utente scorre una notifica a sinistra e la elimina? Sto utilizzando un gestore di allarmi per impostare un avviso ripetuto e ho bisogno che il mio avviso ripetuto si fermi quando la notifica viene annullata dall'utente. Ecco il mio codice:Come rilevare se una notifica è stata respinta?
Impostazione avviso ripetuto:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), repeatFrequency, displayIntent);
mio codice di notifica:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get the notification ID.
int notifID = getIntent().getExtras().getInt("Reminder_Primary_Key");
//Get the name of the reminder.
String reminderName = getIntent().getExtras().getString("Reminder_Name");
//PendingIntent stores the Activity that should be launched when the user taps the notification.
Intent i = new Intent(this, ViewLocalRemindersDetail.class);
i.putExtra("NotifID", notifID);
i.putExtra("notification_tap", true);
//Add FLAG_ACTIVITY_NEW_TASK to stop the intent from being launched when the notification is triggered.
PendingIntent displayIntent = PendingIntent.getActivity(this, notifID, i, Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.flag_red_large, reminderName, System.currentTimeMillis());
CharSequence from = "Here's your reminder:";
CharSequence message = reminderName;
notif.setLatestEventInfo(this, from, message, displayIntent);
//Pause for 100ms, vibrate for 250ms, pause for 100ms, and vibrate for 500ms.
notif.defaults |= Notification.DEFAULT_SOUND;
notif.vibrate = new long[] { 100, 250, 100, 500 };
nm.notify(notifID, notif);
//Destroy the activity/notification.
finish();
}
so che ho bisogno di chiamare alarmManager.cancel(displayIntent)
per annullare la mia sveglia ripetizione. Tuttavia, non capisco dove mettere questo codice. Devo annullare l'avviso di ripetizione SOLO quando l'utente ha toccato la notifica o l'ha respinta. Grazie per l'aiuto!
Non importa, ho trovato la mia risposta qui: http://stackoverflow.com/questions/8811876/notification-deleteintent-does-not-work. – NewGradDev