【发布时间】:2012-06-16 03:38:28
【问题描述】:
我的闹钟要到特定的小时和分钟才会响起。但是,一旦到了那个时间,每次我再次启动应用程序时它都会熄灭。 (当应用程序启动时,它会重置并检查新警报)。
例如,它将在上午 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);
}
}
【问题讨论】:
-
确保您没有在过去设置任何闹钟(例如,今天 9:48 是在 9:48 之后)。
-
看起来这正是我正在做的。我在哪里设置它以忽略不在该分钟、小时和秒内的任何警报?之前还是之后?
标签: android alarmmanager android-pendingintent