【发布时间】:2015-01-17 05:36:16
【问题描述】:
我正在安排事件并每周或每天创建相同的通知。通知正在正确的时间显示。
问题 一旦计划的事件被删除,我想删除通知。但即使事件被删除,我也会收到通知。我尝试通过传递用于创建该意图的相同 unique_id(eventId) 来删除 Pending Intent,但它没有被删除。请在下面找到我的代码:
创建通知
Intent intent = new Intent(this, MyReceiver.class);
HABIT_NAME = habitName.getText().toString();
intent.putExtra("HABIT_NAME", habitName.getText().toString());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), eventId, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
if(daysNo.size() == 7){ //alarm for every day
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,reminderLifeTS.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}else{ // specific days of week
for(int i=0;i<daysNo.size();i++){
int day = getWeek(daysNo.get(i));
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, day);
c.set(Calendar.HOUR_OF_DAY, reminderLifeTS.get(Calendar.HOUR_OF_DAY));
c.set(Calendar.MINUTE, reminderLifeTS.get(Calendar.MINUTE));
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
c.getTimeInMillis(),7*24*60*60*1000, pendingIntent);
}
}
服务类
mManager = (NotificationManager) this.getApplicationContext().
getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
this.getApplicationContext(),0, intent1,PendingIntent.FLAG_ONE_SHOT);
// megha
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("DharmaLife")
.setContentText(AddScheduleEventActivity.HABIT_NAME)
.setContentIntent(pendingNotificationIntent);
Notification note = mBuilder.build();
note.defaults |= Notification.DEFAULT_VIBRATE;
note.defaults |= Notification.DEFAULT_LIGHTS;
note.flags|= Notification.FLAG_AUTO_CANCEL;
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
wakeLock.acquire();
删除通知
Intent intent = new Intent();
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), eventId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent.cancel();
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);
编辑 为了保留创建的所有待处理意图的列表,我将其连同其唯一 ID 一起保存在 arrayList 中:
notificationData.setAlarmManager(alarmManager);
notificationData.setPendingIntent(pendingIntent);
notificationData.setEventId(eventId);
LifeLiteral.NOTIFICATION_STACK.add(notificationData);
删除通知:
for(NotificationData i : LifeLiteral.NOTIFICATION_STACK){
if(i.getEventId() == eventId){
pendingIntent = i.getPendingIntent();
alarmManager = i.getAlarmManager();
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
}
}
但通知仍然没有被删除。请指导。
【问题讨论】:
标签: android notifications android-pendingintent