如何在Android中取消AlarmManager

如何在Android中取消AlarmManager,android,alarmmanager,android-pendingintent,Android,Alarmmanager,Android Pendingintent,我有一个闹钟要到特定的时间才会响。然而,一旦这个时间到来,每次我再次启动应用程序时,它都会关闭。(当应用程序启动时,它会重置并检查新警报) 例如,它将在上午9:48开始播放。在9点48分之前,没有任何事情发生。但在9:48之后,每次应用程序启动时,它都会一直关闭(警报是一个简单的状态栏通知) 这是代码——我哪里出错了 //此处设置报警-每次应用程序启动时都会调用此代码 public void setAlarm() { for (int i : AlarmDays) { Cal

我有一个闹钟要到特定的时间才会响。然而,一旦这个时间到来,每次我再次启动应用程序时,它都会关闭。(当应用程序启动时,它会重置并检查新警报)

例如,它将在上午9:48开始播放。在9点48分之前,没有任何事情发生。但在9:48之后,每次应用程序启动时,它都会一直关闭(警报是一个简单的状态栏通知)

这是代码——我哪里出错了

//此处设置报警-每次应用程序启动时都会调用此代码

public void setAlarm() {
 for (int i : AlarmDays) {

        Calendar cal = Calendar.getInstance();
        if (cal.get(Calendar.DAY_OF_MONTH) > i)
            cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, i);
        cal.set(Calendar.HOUR, 9);
        cal.set(Calendar.MINUTE, 53);
        cal.set(Calendar.SECOND, 1);


        String Name = AlarmNames.get(count);
        count = 0 + 1;

        Intent intent = new Intent(ManageDebts.this, TimeAlarm.class);
        Bundle b = new Bundle();
        b.putString("keyvalue", Name);
        intent.putExtras(b);


        pendingIntent = PendingIntent.getBroadcast(this, i,
                intent, PendingIntent.FLAG_CANCEL_CURRENT);
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                pendingIntent);

    }
 }
TimeAlarm.class

public class TimeAlarm extends BroadcastReceiver {

NotificationManager nm;

@Override
public void onReceive(Context context, Intent intent) {

    String DebtName = intent.getStringExtra("keyvalue");

    nm = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    CharSequence from = "Payment Due: " + DebtName;
    CharSequence message = "Update your Balance Now";
    Intent notificationIntent = new Intent(context, ManageDebts.class);
    notificationIntent.getExtras();
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    Notification notif = new Notification(R.drawable.icon, "Pay "
            + DebtName + " today!", System.currentTimeMillis());
    notif.setLatestEventInfo(context, from, message, contentIntent);
    notif.defaults = Notification.DEFAULT_SOUND
            | Notification.DEFAULT_LIGHTS;
    notif.flags = Notification.FLAG_SHOW_LIGHTS;
    notif.ledOnMS = 100;
    notif.ledOffMS = 100;
    notif.flags |= Notification.FLAG_AUTO_CANCEL;
    nm.notify(1, notif);
}
}

我在哪里设置它来忽略任何不在那一分钟、小时和秒内的警报?之前还是之后


setAlarm()
方法中。在进入循环之前,可以从
AlarmDays
中筛选出内容,或者将
cal.getTimeInMillis()
System.currentTimeMillis()
进行比较,以查看它是否在过去,或者类似的情况。

确保您没有在过去设置任何报警(例如,9:48之后的今天是9:48)1.看起来我就是这么做的。我在哪里设置它来忽略任何不在那一分钟、小时和秒内的警报?之前还是之后?谢谢。我用了一个比较if语句。工作得很好。