设置多个警报android的问题?

时间:2013-10-29 08:25:23

标签: android android-alarms android-broadcast

我正在尝试使用广播接收器设置多个闹钟。我在每1分30秒后设置两次报警重复但是当我收到报警时,两者都在同时重复。

我这样做:

if(repeat.equals("No Repeat")){
                    bcr.CancelAlarm(getApplicationContext());
                }
                else if(repeat.equals("After 10 Minutes")) {
                    bcr.TenMintAlarm(getApplicationContext(), hour, minute, dueDate,task_note,Tasks_ID);
                }
                else if(repeat.equals("After 15 Minutes")){
                    bcr.FifteenMintAlarm(getApplicationContext(), hour, minute, dueDate,task_note,Tasks_ID);
                }
                else if(repeat.equals("After n Minutes")){
                    bcr.nMintAlarm(getApplicationContext(), hour, minute, dueDate,task_note,Tasks_ID);
                }

广播接收器:

public void CancelAlarm(Context context)
{
    Intent intent = new Intent(context, AlarmReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);
}
public void TenMintAlarm(Context context,int hour,int minutes, String duedate,String task_note,int task_id) 
{
    AlarmManager Alarm1 = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra("NOTE", note);
    intent.putExtra("ID", task_id);
    int mUniqID = task_id+2;
    PendingIntent recurringAlarm = PendingIntent.getBroadcast(context, mUniqID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    System.out.println("mUniqID-----" +mUniqID);
    Alarm1.setRepeating(AlarmManager.RTC_WAKEUP, _alarm, 1000 * 60, recurringAlarm);
}
public void FifteenMintAlarm(Context context, int hour,int minutes, String duedate, String task_note, int task_id)
{
    AlarmManager Alarm2 = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, AlarmReceiver.class);
    i.putExtra("NOTE", note);
    i.putExtra("ID", task_id);
    int uniqID = task_id+2;
    System.out.println("mUniqID-----" +uniqID);
    PendingIntent Alarm = PendingIntent.getBroadcast(context, uniqID, i, PendingIntent.FLAG_UPDATE_CURRENT);
    Alarm2.setRepeating(AlarmManager.RTC_WAKEUP, _alarm, 1000 * 30, Alarm);
}

但是当收到警报时,它会同时重复,而不是我希望在1分钟后重复一次警报,并在30秒后重复一次。任何建议都可能有所帮助。感谢。

2 个答案:

答案 0 :(得分:0)

根据您的else-if块,根据您指定的条件,只会设置一个警报。如果你想设置2个警报,那么你应该将它们放在同一个块或其他东西中。

答案 1 :(得分:0)

您需要为pending intent使用不同的ID。您在两个函数中使用的mUniqIDuniqID相等,make BroadcastReciever认为它与其他函数相同,因此它们会同时触发。只需将它们设置为不同的数字。

public void TenMintAlarm(Context context,int hour,int minutes, String duedate,String task_note,int task_id) 
{
    AlarmManager Alarm1 = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra("NOTE", note);
    intent.putExtra("ID", task_id);
    int mUniqID = 20;//change in here
    PendingIntent recurringAlarm = PendingIntent.getBroadcast(context, mUniqID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    System.out.println("mUniqID-----" +mUniqID);
    Alarm1.setRepeating(AlarmManager.RTC_WAKEUP, _alarm, 1000 * 60, recurringAlarm);
}
public void FifteenMintAlarm(Context context, int hour,int minutes, String duedate, String task_note, int task_id)
{
    AlarmManager Alarm2 = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, AlarmReceiver.class);
    i.putExtra("NOTE", note);
    i.putExtra("ID", task_id);
    int uniqID = 10;//change in here
    System.out.println("mUniqID-----" +uniqID);
    PendingIntent Alarm = PendingIntent.getBroadcast(context, uniqID, i, PendingIntent.FLAG_UPDATE_CURRENT);
    Alarm2.setRepeating(AlarmManager.RTC_WAKEUP, _alarm, 1000 * 30, Alarm); 
}