如何在一段时间后取消闹钟? |安卓

时间:2013-12-17 13:31:41

标签: android alarmmanager android-alarms

嗨我在这里有一个警报,我希望有一种方法可以在一段时间后取消它,例如一天或一周,,,并提前感谢,

    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);

3 个答案:

答案 0 :(得分:1)

设置第二个警报,取消第一个警报。

答案 1 :(得分:1)

final PendingIntent sender = PendingIntent.getBroadcast(parent.getContext(), CountMultipleAlarm,
                                     alarmintent,0);
int someTime=1000;

new Handler().postDelayed(new Runnable()
{
    public void run()
    {
     am.cancel(sender);
    }
}, someTime);

答案 2 :(得分:0)

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

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