2013-05-26 55 views
4

我使用AlarmManager在事件日期和时间显示事件通知。 但是,当事件更新时,如何更新AlarmManager将PendingIndent发送到我的应用程序的时间?AlarmManager(Android)的更新时间

当一个事件被产生的下面的代码被称为:

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); 
    } 

回答

7

我发现溶液..我原来,第二个参数getBroadcast(Context context, int requestCode, Intent intent, int flags)即使在文档中提到

requ estCode发件人的私人请求代码(目前未使用)。

当每个事件使用一个请求ID时,更新报警并为每个事件创建报警。

原因是,有两个不同的请求代码,filterEquals(Intent)将是错误的。 AlarmManager set(...)的说明文档如下:

如果时间发生在过去,报警将立即触发 。如果已经有由 filterEquals(Intent))被定义为这个意图安排 报警(有两个意图的平等,那么它会被删除并以此 更换。

我也改变PendingIntent.FLAG_ONE_SHOTPendingIndent.FLAG_CANCEL_CURRENT