我正在开发一个android应用程序。在这个应用程序中,我试图设置多个警报与日期-时间从用户获取。我知道这可以使用广播接收器来完成。下面是我尝试过的代码:
private void setReminder(Calendar time){
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
alarmIntent.putExtra("name",teacherName);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, 0);
alarmManager.set(AlarmManager.RTC,time.getTimeInMillis(),pendingIntent);
}但问题是,它只适用于一个警报。当我设置多个警报时,警报响应是用户用第一个警报的结果设置的最后一次。我应该怎么做才能正确地触发所有警报?一个完整的示例代码,对我有很大帮助。提前感谢
发布于 2017-05-01 13:38:22
添加多个告警,如:
private void setReminder(Calendar time){
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
alarmIntent.putExtra("name",teacherName);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), getRandom(), alarmIntent, 0);
alarmManager.set(AlarmManager.RTC,time.getTimeInMillis(),pendingIntent);
}getRandon()函数
private int getRandom() {
Random random = new Random();
return random.nextInt(99999) + 1;
}发布于 2017-05-01 13:39:12
试试这段代码。使用for循环进行多个告警
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
ArrayList<PendingIntent> intentsArrayList = new ArrayList<PendingIntent>();
for(i = 0; i < 5; ++i)
{
Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
// i the request code for every alarm with current position
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), i, alarmIntent, 0);
alarmManager.set(AlarmManager.RTC,
time.getTimeInMillis() + 60000 * i,
pendingIntent);
alarmIntent.putExtra("name",teacherName+i);
intentsArrayList.add(pendingIntent);
}https://stackoverflow.com/questions/43714700
复制相似问题