在下面的代码中,它正在做我目前想要做的主要事情: - 每小时同时重复
有人可以验证我是否或需要做什么来确保以下项目?
(1) 警报最终将基于一个月中的天数。只要他们唤醒手机时它会熄灭(以节省电池)。它不是特定于小时或分钟的。只有一天。如何做到这一点?
(2)如果Activity被销毁或者手机重启,不知道我的AlarmManager是否保持清醒?
(3) 最后,每次应用程序启动时都会重复此代码(从而覆盖任何现有的 AlarmManager;这是一种正确的做事方式,还是我应该检查是否存在警报?
for (int i : AlarmDays) {
if (String.valueOf(i) == null ) {
continue;
}
Calendar cal = Calendar.getInstance();
if (cal.get(Calendar.MINUTE) >= i)
cal.add(Calendar.HOUR, 1);
cal.set(Calendar.MINUTE, i);
Intent intent = new Intent(this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, i,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
60 * 60 * 1000, pendingIntent);
}
// TimeAlarm.class
public void onReceive(Context context, Intent intent) {
String DebtName = null;
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Payment Due";
CharSequence message = "Open App and Update your Balance";
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;
nm.notify(1, notif);
}
在我的清单中的应用程序标签中:
编辑:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<Application>
<receiver
android:name=".TimeAlarm"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</Application>