I want to use alarms to trigger some processing.
In the code below, I have tried to set two alarms. They trigger a Service which displays a Notification indicating the id number of the alarm which has just gone off.
When the first Alarm goes off, a Notification should appear and display : “1”.
For the second Alarm, the Notification should show “2”.
As described in the Javadoc, only one alarm is triggered (they have the same Intent, so the second one replaces the first one). But what is strange is that the Notification displays “1” and appears at the time when the second Alarm goes off!!! It seems that the time for the alarm to go off is modified but not the extras.
Any idea?
Thanks in advance for the time you will spend trying to help me.
public class TestAlarmes extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
AlarmManager am;
Intent action;
PendingIntent intent;
long t0;
RecepteurMessageAlarme récepteur;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// créer le récepteur de message d'alarme
récepteur=new RecepteurMessageAlarme();
// et l'enregistrer en lui demandant de filtrer sur les messages d'alarme
registerReceiver(récepteur,new IntentFilter("GL.TestAlarmes.ALARME"));
// récupérer le service d'alarme
am=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
// créer l'action 1
action=new Intent(this,ServiceAlarme.class);
action.setAction("GL.TestAlarmes.ALARME");
// personnaliser l'action a exécuter
action.putExtra("GL.TestAlarmes.Action",1);
// créer l'intent à lancer lors du déclenchement de l'alarme
intent=PendingIntent.getService(this,0,action,PendingIntent.FLAG_ONE_SHOT);
// prendre l'instant présent
t0=java.lang.System.currentTimeMillis();
// configurer l'alarme
am.set(AlarmManager.RTC_WAKEUP,t0+5000,intent);
// créer l'action 2
action=new Intent(this,ServiceAlarme.class);
action.setAction("GL.TestAlarmes.ALARME");
// personnaliser l'action a exécuter
action.putExtra("GL.TestAlarmes.Action",2);
// créer l'intent à lancer lors du déclenchement de l'alarme
intent=PendingIntent.getService(this,0,action,PendingIntent.FLAG_ONE_SHOT);
// configurer l'alarme
am.set(AlarmManager.RTC_WAKEUP,t0+10000,intent);
}
}
Try giving different request codes for
PendingIntentto make it unique