在尝试将警报管理器设置为在满足条件的特定时间运行时遇到了一些麻烦。对于这部分代码,我正在检查事件日期是否是当前日期的前一天:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String currentDate = dateFormat.format(new Date());
for (int count = 0; count < _eventlist.size(); count++) {
Date alarmDate = null;
try {
alarmDate = dateFormat.parse(_eventlist.get(count)
.getEventDate());
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(alarmDate);
calendar.add(Calendar.DATE, -1);
Date alarmBeforeOneDay = calendar.getTime();
Log.i("Alarm", dateFormat.format(alarmBeforeOneDay));
calendar.add(Calendar.DATE, 1);
if (dateFormat.format(alarmBeforeOneDay).equals(currentDate)) {
if (!AlarmInitialized(context)) {
scheduleAlarms(context);
}
}
}
它符合条件,它将执行scheduleAlarms()以发送推送通知:
public static void scheduleAlarms(Context context) {
/* Calendar calendar = Calendar.getInstance();
long firstRunTime = calendar.getTimeInMillis();*/
Log.i("Schedule","Alarm");
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 00);
cal.set(Calendar.MINUTE, 30);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context, Alarm.class);
notificationCount = notificationCount + 1;
notificationIntent.putExtra("NotifyCount", notificationCount);
PendingIntent pi = PendingIntent.getBroadcast(context, 1,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
BootReceiver.class:
public void onReceive(Context context, Intent i) {
if (i.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
ProfileUpcomingEvent.scheduleAlarms(context);
}
}
Alarm.class:
public void onReceive(Context context, Intent intent) {
mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(context, 1,
new Intent(), 0);
notification = new Notification(R.drawable.ic_launcher,
"Notification", System.currentTimeMillis());
notification.setLatestEventInfo(context, "Event Reminder",
"Registered event at WHERE is tomorrow!",
contentIntent);
mNotificationManager.notify(
Integer.parseInt(intent.getExtras().get("NotifyCount")
.toString()), notification);
ProfileUpcomingEvent.updateAlarmLastRun(context);
}
的AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".Alarm" >
</receiver>
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="false"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
所以在这种方法中,如果满足条件,我试着将其设置为00:30运行。但是,我们说我将eventDate设置为26/11/2014。它应该在2014年11月25日00:30之前向我发送推送通知,这是在eventDate前一天,但事实并非如此。我想知道为什么它不会运行,因为我已经使用Log检查了日期格式,这是正确的。
提前致谢。