我已经完成了关于这个主题的所有帖子,我觉得我的代码尽可能接近发布的解决方案。但我的警惕者并没有始终如一地开始。如果时间是过去的,它没有问题,并且事件记录在我的日志文件中。它永远不会在第二天发生。有时是在当天几小时之外的活动。
以下是创建AlarmManager的代码:
public void createNotifications(ArrayList alertList){
ResortActivity activity;
int arryLen = alertList.size();
for (int i = 0; i < arryLen; ++i){
activity = (ResortActivity) alertList.get(i);
if (activity.getActAlert().equals("Y")){
//Loop through array list checking if alert set to Y if so create alert
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE);
// Every scheduled intent needs a different ID, else it is just executed once
int id = (int) System.currentTimeMillis();
// Prepare the intent which should be launched at the date
//Intent intent = new Intent(this, ActivityNotifier.class);
//Intent intent = new Intent(this, TimeAlarm.class);
Intent intent = new Intent(this, ActivityNotifier.class);
//TODO check if object can be passed and pass ResortActivity
intent.putExtra ("Alert_ID", activity.getActId());
// Prepare the pending intent
Integer actIntID = new Integer(activity.getActId());
id = actIntID.intValue();
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Register the alert in the system. You have the option to define if the device has to wake up on the alert or not
Calendar actCal = activity.getCalendar();
System.out.println("Activity Date : "
+ actCal.get(Calendar.MONTH) + "/"
+ actCal.get(Calendar.DAY_OF_MONTH) +"/"
+ actCal.get(Calendar.YEAR) + " "
+ actCal.get(Calendar.HOUR_OF_DAY)
+ ":"
+ actCal.get(Calendar.MINUTE)
+ ":"
+ actCal.get(Calendar.SECOND)
+ actCal.get(Calendar.AM_PM));
System.out.println(actCal.toString());
actCal.add(Calendar.MINUTE, -15);
System.out.println(actCal.toString());
System.out.println("After 15 min : "
+ actCal.get(Calendar.MONTH) + "/"
+ actCal.get(Calendar.DAY_OF_MONTH) +"/"
+ actCal.get(Calendar.YEAR) + " "
+ actCal.get(Calendar.HOUR_OF_DAY)
+ ":"
+ actCal.get(Calendar.MINUTE)
+ ":"
+ actCal.get(Calendar.SECOND)
+ actCal.get(Calendar.AM_PM));
//alarmManager.set(AlarmManager.RTC_WAKEUP, activity.getCalendar().getTimeInMillis(), pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, actCal.getTimeInMillis(), pendingIntent);
}//end if for alert = "Y"
}
}//createNotifications
这是接收警报的类
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ResortPlanner");
//Acquire the lock
wl.acquire();
System.out.println("here in onReceive");
ResortActivity activity = null;
final GlobalVariables globalVariables = (GlobalVariables) context.getApplicationContext();
int actID;
actID = new Integer(intent.getStringExtra("Alert_ID")).intValue();
Logger tlogger = new Logger();
tlogger.setLogFilename(globalVariables.getLogFilename());
tlogger.setLogClass("ActivityNotifier");
tlogger.setLogMsg("ID: " + actID);
tlogger.setLogProcedure("Activity Notifier");
tlogger.setLogType("I");
tlogger.logMessage();
ActivitiesReader actReader = new ActivitiesReader(globalVariables.getActAlertFullName());
try{
activity = actReader.getActivity(actID);
}catch (ResortAppsException e){
Logger logger = new Logger();
logger.setLogFilename(globalVariables.getLogFilename());
logger.setLogClass(e.getErrClass());
logger.setLogMsg(e.getErrMsg());
logger.setLogProcedure(e.getErrProcedure());
logger.setLogType("E");
logger.logMessage();
logger = new Logger();
logger.setLogFilename(globalVariables.getLogFilename());
logger.setLogClass("ActivityNotifier");
logger.setLogMsg("Unable to create notified due to previous error.");
logger.setLogProcedure("onReceive");
logger.setLogType("E");
logger.logMessage();
}
// Request the notification manager
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.BigTextStyle bigTxtStyle =
new NotificationCompat.BigTextStyle();
bigTxtStyle.bigText(activity.getActTime() + " " + activity.getActDescr());
bigTxtStyle.setBigContentTitle("Activity Reminder");
NotificationCompat.Builder noteBuild = new NotificationCompat.Builder(context)
.setContentTitle("Activity Reminder")
.setContentText(activity.getActTime() + " " + activity.getActDescr())
.setSmallIcon(R.drawable.notification)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_VIBRATE + Notification.DEFAULT_SOUND)
.setStyle(bigTxtStyle);
// Fire the notification
Notification notify = noteBuild.build();
notificationManager.notify(actID, notify);
//notificationManager.notify(1, notification);
wl.release();
}
提前感谢您提供的任何帮助。