I'm trying to use the alarmmanager
for my android app.
if the username change, I wanna cancel the current alarm and set a new one for the current username
The problem is that am.cancel(sender);
doesn't cancel the alarm
Also, I don't see the new alarm for the new username.
Here's my code
private AlarmManager am;
private PendingIntent sender;
public void registerAlarm() {
//Cancel the current alarm
if(am != null){
am.cancel(sender);
}
Intent i = new Intent(this, AlarmReceiver.class);
i.putExtra("username", username);
sender = PendingIntent.getBroadcast(this, 0, i, 0);
// We want the alarm to go off 3 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += 3 * 1000;//start 3 seconds after first register.
// Schedule the alarm!
am = (AlarmManager) this
.getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
60000, sender);//10min interval
}
}
Thank you
EDIT
I also tried this code without success
public void SetAlarm(Context context, String username)
{
//Cancel the current alarms
CancelAlarm(context);
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, AlarmReceiver.class);
i.putExtra("username",username);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
// We want the alarm to go off 3 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += 3 * 1000;//start 3 seconds after first register.
// Schedule the alarm!
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
60000, pi);//1min interval
}
public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
Aucun commentaire:
Enregistrer un commentaire