【发布时间】:2016-07-20 19:03:01
【问题描述】:
在我的活动中,我为 AlarmManager 设置了在未来日期生成通知的时间,并将信息存储在 sqlite db 中。通过更改 id 来通知 notificationManager 设置多个通知后,我在 ListView 中显示信息。
现在我想取消或删除一些通知。例如,我将通知时间设置为 1 小时后,我想在 listview 的 longitemclick 上取消它,但通知会生成。
我看到了如何从通知栏取消通知,但这不适合我的情况。我想在通知生成之前取消它。
我怎样才能做到这一点?
这是我的呼叫通知代码:
custom = new CustomDateTimePicker(noticall.this,new CustomDateTimePicker.ICustomDateTimeListener() {
@Override
public void onCancel() {
finish();
}
@Override
public void onSet(Dialog dialog, Calendar calendarSelected,Date dateSelected, int year, String monthFullName,
String monthShortName, int monthNumber, int date,
String weekDayFullName, String weekDayShortName,
int hour24, int hour12, int min, int sec,
String AM_PM) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, monthNumber, date, hour24, min, 0);
long when = calendar.getTimeInMillis(); // notification time
myIntent = new Intent(noticall.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(noticall.this, 0, myIntent,0);
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
Date = calendarSelected.get(Calendar.DAY_OF_MONTH) + "/" + (monthNumber+1) + "/" +year;
Time = hour12 + ":" + min + " " + AM_PM;
setlisto(Date);
Intent intent = getIntent();
finish();
startActivity(intent);
Toast.makeText(noticall.this,"Record Added", Toast.LENGTH_SHORT).show();
}
});
MYRECIEVER 类:
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent)
{
/*Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);*/
Log.i("App", "called receiver method");
try{
Utils.generateNotification(context);
}catch(Exception e){
e.printStackTrace();
}
}
}
实用程序类:
public class Utils {
//int k = noticall.in2;
static int i=0;
static long[] vibrate = { 1000, 1000, 1000, 1000, 1000 };
public static NotificationManager mManager;
@SuppressWarnings("static-access")
public static void generateNotification(Context context){
mManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
int j=i+=1;
Intent intent1 = new Intent(context,Sipdroid.class);
Notification notification = new Notification(R.drawable.yel1," NOTIFICATION!", System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context,0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, "xxxx", "xx "+noticall.Names, pendingNotificationIntent);
Log.i("increeeee", noticall.incre+"");
mManager.notify(j, notification); // for multiple notifications... change 0 to no of notification....
}
}
在我的这个活动中,我想取消设置的通知:
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
Log.w("DELETED", " DELETED");
// code here to delete specific notification or pendingIntent or alarmmanager
}
});
我尝试了此代码,但它可以取消所有待定意图以取消我想取消特定的pendingIntents:
public void unsetAlarm(View v) {
myIntent = new Intent(noticall.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(noticall.this, 0, myIntent,0);
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Log.v("ahooooo", "cancelling notification");
}
如何取消特定的pendingIntents?
【问题讨论】:
标签: android sqlite notifications broadcastreceiver alarmmanager