简体   繁体   中英

stop Alarm Manager at specifc time

I am triggering this alarm Manager using Service. So I want to stop the alarm Manager after 30 minutes, as it repeats after 10 minutes. Cancel doesn't work in my case please help.

             AlarmManager alarmMgr0 = (AlarmManager) 
             getSystemService(Context.ALARM_SERVICE);

            Intent intent0 = new Intent(getApplicationContext(), 
            schedule.class);
            PendingIntent pendingIntent0 = 
            PendingIntent.getBroadcast(getApplicationContext(), 0, intent0, 0);
            Log.e(TAG, "SiteIN called");

            // Intent intent0 = new Intent(this, OldEntryRemover.class);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE,26);
            calendar.set(Calendar.SECOND, 0);

            if(Calendar.getInstance().after(calendar)){
        // Move to tomorrow
            calendar.add(Calendar.DATE, 1);
            }


    //set that timer as a RTC Wakeup to alarm manager object
            // alarmMgr0.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent0);
            alarmMgr0.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                    10000 * 60, pendingIntent0);

              try {
                 thread.sleep(30000 * 60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
           alarmMgr0.cancel(pendingIntent0);

I'd look at the API and see if this is truly what you want.

Schedule a repeating alarm. Note: for timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler . If there is already an alarm scheduled for the same IntentSender, it will first be canceled.

Like set(int, long, PendingIntent), except you can also supply a period at which the alarm will automatically repeat. This alarm continues repeating until explicitly removed with cancel(AlarmManager.OnAlarmListener). If the stated trigger time is in the past, the alarm will be triggered immediately, with an alarm count depending on how far in the past the trigger time is relative to the repeat interval.

If an alarm is delayed (by system sleep, for example, for non _WAKEUP alarm types), a skipped repeat will be delivered as soon as possible. After that, future alarms will be delivered according to the original schedule; they do not drift over time. For example, if you have set a recurring alarm for the top of every hour but the phone was asleep from 7:45 until 8:45, an alarm will be sent as soon as the phone awakens, then the next alarm will be sent at 9:00.

If your application wants to allow the delivery times to drift in order to guarantee that at least a certain time interval always elapses between alarms, then the approach to take is to use one-time alarms, scheduling the next one yourself when handling each alarm delivery.

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