0

我想在2个特定时间内在我的应用程序中每天显示2个通知,直到现在我只能够显示一个通知。 这是我的代码,我如何显示多个通知。 一个在上午7点,另一个在下午6点例如?在Android中使用闹钟管理器每天显示多个通知

 Intent myIntent = new Intent(Calender.this, MyAlarmService.class); 
     int id = (int) System.currentTimeMillis(); 

     pendingIntent = PendingIntent.getService(Calender.this, id, 
       myIntent, Notification.FLAG_ONLY_ALERT_ONCE); 

     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

     Calendar timeToSet = Calendar.getInstance(); 
     timeToSet.set(Calendar.HOUR_OF_DAY, hour); 

     alarmManager.set(AlarmManager.RTC_WAKEUP, 
       timeToSet.getTimeInMillis(), pendingIntent); 

我在OnStart方法

 final Calendar c = Calendar.getInstance(); 


    Notification note = new Notification(R.drawable.icon, 
      getString(R.string.app_name), System.currentTimeMillis()); 
    Intent intent = new Intent(this, Calender.class); 
    PendingIntent i = PendingIntent.getActivity(this, 0, intent, 
      Notification.FLAG_ONGOING_EVENT); 
    note.setLatestEventInfo(this, getString(R.string.app_name), 
      "Some String", i); 
    note.flags |= Notification.FLAG_AUTO_CANCEL; 
    NOTIFY_ME_ID = System.currentTimeMillis(); 
    mgr.notify((int) NOTIFY_ME_ID, note); 
+0

设置警报管理器两次。 –

+0

我应该调用所有的代码行再次设置警报或alarmManager.set(AlarmManager.RTC_WAKEUP, timeToSet.getTimeInMillis(),pendingIntent); ? – Reham

+0

我认为是的,再次定义'timeToSet',这次例如是6PM,并为新的时间设置'alarmManager.set(....)'。 –

回答

0

你应该设定不同的通知不同的密钥,称这在MyAlarmService。如果您使用一个键进行多次通知,它将重写相同的键。

+0

我已经编辑了我的答案,请检查) – pol