我想用廣播接收機設置多個鬧鐘。每兩分鐘報警1分30秒後重復設置,但當我收到報警時,兩個報警同時重複。問題在設置多個鬧鐘android?
我這樣做,如:
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秒後。任何建議可能會有所幫助。謝謝。
你確定這個機器人警報叫嗎? –
我擔心'PendingIntent'中的兩個警報ID總是相同,因此會覆蓋另一個警報。如果您想要有多個警報,請先檢查ID是否有所不同。 –
@AM,是的,我收到的兩個警報都是準時的,但是問題都是同時重複的,但我有兩個不同的值,一個是四個30秒,另一個是1分鐘。但兩分鐘後都會重複。 – user2822311