我是Android编程新手。我正在开发一个有本地通知的应用程序。我正在使用Alarmmanager设置本地通知。 当我将同一时间设置为Alarmmanager时,onReceive会被调用一次。我的未决意图也不同。
这里我是如何设置闹钟的:
for(int i = 0; i < dosageDtoList.size();i++)
{
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
DosageDto dosageDto = dosageDtoList.get(i);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time = dosageDto.getReminderTime();
String dateTime = startDateStr + " " + time;
Date formattedDate = simpleDateFormat.parse(dateTime);
Calendar cal = Calendar.getInstance();
cal.setTime(formattedDate);
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.addCategory("android.intent.category.DEFAULT");
notificationIntent.putExtra(Constants.POS,i);
notificationIntent.putExtra(Constants.END_DATE, endDateStr);
notificationIntent.putExtra(Constants.TITLE, userDrugDto.getUserName());
notificationIntent.putExtra(Constants.SUB_TITLE, userDrugDto.getDrugName());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent broadcast = PendingIntent.getBroadcast(context, i, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if(alarmManager != null)
{
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, broadcast);
}
}
这是我的Receiver类:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// generating notification
}
}
如果我为这两个日期和时间设置闹钟 1. 01-June-2018 15:10 2. 01-June-2018 15:10
理想情况下,onReceive具有不同的待处理意图应该被调用两次,但它被调用一次。
我错过了什么吗?