简体   繁体   中英

How to make a non repeated alarm in android?

I want to make an alarm to be triggered at specific time and this alarm will be only once " non repeated" , I have used the following alarm manager method ,but it repeats daily !! how can i prevent the alarm from repeating ?? please help me ..

final PendingIntent sender = PendingIntent.getBroadcast(this.ctx, 0, intent,        PendingIntent.FLAG_CANCEL_CURRENT);
final AlarmManager am = getAlarmManager();
am.set(AlarmManager.RTC_WAKEUP,getTriggerTime(), sender);

Thanks in advance.

It shouldn't be repeating since you are using the AlarmManager.set method. Are you sure you are not scheduling it every time you open the app? If the code snippet you are showing is running every time you open the app or running periodically in a background service, then it will schedule an alarm every time it executes.

If this is the case, then you should check some sort of flag that indicates whether the alarm is already scheduled once and prevent this code from running again if it is.

Use this:

pendingIntent = PendingIntent.getService(
                                AndroidAlarmSMS.this, 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();



alarmManager.set(AlarmManager.RTC_WAKEUP,
                                    calendar.getTimeInMillis(), pendingIntent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM