0

我有一个闹钟,直到特定的小时和分钟才会响起。但是,一旦到了那个时间,每次我再次启动应用程序时它都会熄灭。(当应用程序启动时,它会重置并检查新警报)。

例如,它将在上午 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);
}

}

4

1 回答 1

1

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

在你的setAlarm()方法中。AlarmDays在进入循环之前过滤掉一些东西,或者比较它cal.getTimeInMillis()是否System.currentTimeMillis()是过去的,或者类似的东西。

于 2012-06-15T19:38:28.867 回答