2013-01-24 4 views
1
public void SetAlarm(Context context, int sec) 
{ 
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    Intent i = new Intent(context, Alarm.class); 
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); 
    am.set(AlarmManager.RTC, System.currentTimeMillis()+1000*5 , pi); 
    i = new Intent(context, Alarm.class); 
    pi = PendingIntent.getBroadcast(context, 0, i, 0); 
    am.set(AlarmManager.RTC, System.currentTimeMillis()+1000*10 , pi); 

} 

왜 onReceive는 10 초 후에 한 번 작동합니까?Android AlarmManager는 한 번만 작동합니다.

답변

6

경보 관리자는 동일한 정보로 보류중인 의도가 경보 관리자에 제공 되었기 때문에 첫 번째 경보를 취소합니다. 인 텐트가이 필터와 일치하는 모든 유형의 알람 (filterEquals (Intent)에 정의 된대로)은 취소됩니다.

여러 알람 (반복 또는 단일)을 설정하려면 다른 requestCode로 PendingIntents를 만들어야합니다. requestCode가 같으면 새 알람이 이전 알람을 덮어 씁니다.

이 함께 시도

...

public void SetAlarm(Context context, int sec) 
{ 
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
Intent i = new Intent(context, Alarm.class); 
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); 
am.set(AlarmManager.RTC, System.currentTimeMillis()+1000*5 , pi); 
i = new Intent(context, Alarm.class); 
pi = PendingIntent.getBroadcast(context, 1, i, 0); // new request code 
am.set(AlarmManager.RTC, System.currentTimeMillis()+1000*10 , pi); 

} 
+0

한 클래스 수신기 - 한 알람? – Hemul

+0

동일한 의도 데이터가있는 두 개의 알람을 설정합니다. 따라서 최신 경보는 이전 경보를 취소합니다. – Jambaaz

+0

감사합니다. 작동합니다! :) – Hemul

관련 문제