AlarmManager(Android)的更新时间
我使用AlarmManager在事件日期和时间显示事件通知。 但是,当更新事件时,我如何更新AlarmManager将PendingDent发送到我的应用程序的时间 创建事件时,将调用以下代码:AlarmManager(Android)的更新时间,android,notifications,alarmmanager,Android,Notifications,Alarmmanager,我使用AlarmManager在事件日期和时间显示事件通知。 但是,当更新事件时,我如何更新AlarmManager将PendingDent发送到我的应用程序的时间 创建事件时,将调用以下代码: public void setOneTimeAlarm() { Intent intent = new Intent(this, TimerReceiver.class); PendingIntent pendingIntent = PendingIntent.getBr
public void setOneTimeAlarm() {
Intent intent = new Intent(this, TimerReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, day-1);
c.set(Calendar.HOUR_OF_DAY, 18);
c.set(Calendar.MINUTE, 00);
long date = c.getTimeInMillis();
mAlarmManager.set(AlarmManager.RTC_WAKEUP, date, pendingIntent);
}
名为TimerReciver的缩进:
@Override
public void onReceive(Context context, Intent intent) {
Log.v("TimerReceiver", "onReceive called!");
Intent notificationIntent = new Intent(context, ListTests.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
123, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = context.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
long[] pattern = {0,300};
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker(res.getString(R.string.app_name))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setVibrate(pattern)
.setContentTitle(res.getString(R.string.notification_title))
.setContentText(res.getString(R.string.notification_text));
Notification n = builder.build();
nm.notify(789, n);
}
我找到了解决办法。。我发现,
getBroadcast(Context-Context,int-requestCode,Intent-Intent,int-flags)
的第二个参数起到了作用,即使文档中说
requestCode发件人的专用请求代码(当前未使用)
当为每个事件使用请求id时,将更新报警并为每个事件创建报警
原因是,对于两个不同的请求代码,filterEquals(Intent)
将为false。AlarmManager集合(…)
的文档说明:
如果时间发生在过去,则会触发报警
马上。如果已经有此目的的警报
(两个意图相等的定义如下:
filterEquals(Intent))
,然后将其删除并替换为该
一个
我还将PendingIntent.FLAG_ONE_SHOT
更改为PendingIndent.FLAG_CANCEL_CURRENT