一段时间后如何取消警报? |安卓 [英] How i can cancel an alarm after a period of time? | android

查看:169
本文介绍了一段时间后如何取消警报? |安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有个警报,我想有一种方法可以在一段时间(例如一天或一周后)之后将其取消,

hi i have an alarm here and i want a way to cancel it AFTER a period of time like a day for example or a week ,,, and thanks in advance ,,

    am = (AlarmManager)parent.getContext().getSystemService(Context.ALARM_SERVICE);

     //the title and the description of the notification



    Intent alarmintent = new Intent(parent.getContext(), Alarm_Receiver.class);
     alarmintent.putExtra("title",titlePills + dea22);
      alarmintent.putExtra("note",dea22);
     alarmintent.putExtra("NOTIFICATION_ID",String.valueOf(CountMultipleAlarm));
     //HELLO_ID is a static variable that must be initialized at the BEGINNING OF CLASS with 1;

      //example:protected static int HELLO_ID =1;
      PendingIntent sender = PendingIntent.getBroadcast(parent.getContext(), CountMultipleAlarm,
                                     alarmintent,0);

      //VERY IMPORTANT TO SET FLAG_UPDATE_CURRENT... this will send correct extra's informations to 
       //AlarmReceiver Class
         // Get the AlarmManager service
        am.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), 11000, sender);
                                     intentArray.add(sender);


推荐答案

您已经设置了一次警报,所以您知道怎么做。关键是设置第二个警报,其目的是结束原始警报。困难的部分是找到要取消的原始警报,但是,您可以使用取消待处理的意图。如Android文档所指定,此代码将取消所有具有相同类的警报。因此,您只需要创建一个意图,然后将其传递给AlarmManager,就可以取消警报。

You have already set an alarm once, so you know how to do it. The key is to set a second alarm, whose purpose is to end the original alarm. The difficult part is finding the original alarm to cancel, however, you can just execute this code upon your second alarm to cancel the first, using the cancel pending intent. This code, as Android docs specify, will cancel all alarms with the same class. Thus, you only need to create an intent, pass it to the AlarmManager, and you can cancel the alarm.

Intent alarmintent = new Intent(parent.getContext(), Alarm_Receiver.class);
PendingIntent sender = PendingIntent.getBroadcast(parent.getContext(), CountMultipleAlarm,
                                 alarmintent,0);
am.cancel(sender);

这篇关于一段时间后如何取消警报? |安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆