我的应用程序中有这个闹钟设置功能,用户可以为目标上的每个活动设置闹钟。该功能在我设置的最初几个警报中起作用,但在此之后,它不再发出警报。我不知道为什么。我认为它与AlarmManager有关。我找不到解决这些问题的任何消息来源。
这是我的代码:
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
MyDBAdapter dbhandler;
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
//Acquire the lock
wl.acquire();
//You can do the processing here.
Bundle extras = intent.getExtras();
StringBuilder msgStr = new StringBuilder();
//Release the lock
wl.release();
}
public void SetAlarms(Context context, int goal_id)
{
dbhandler = new MyDBAdapter(context);
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
List<Activities> allActs = dbhandler.getAllActivitiesByGoal(goal_id);
for (final Activities act : allActs) {
//for alarm
//Create an offset from the current time in which the alarm will go off.
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.DATE, Integer.parseInt(act.getEDay())); //1-31
cal.set(Calendar.MONTH, Integer.parseInt(act.getEMonth()) - 1); //first month is 0!!! January is zero!!!
cal.set(Calendar.YEAR, Integer.parseInt(act.getEYear()));//year...
cal.set(Calendar.HOUR_OF_DAY, act.getHour());
cal.set(Calendar.MINUTE, act.getMinute());
cal.set(Calendar.SECOND, act.getSecond());
cal.set(Calendar.MILLISECOND, act.getMillisecond());
//MessageTo.message(SetNotifyActivity.this, hour+":"+minute+" "+format);
//assigned a unique id to notifications
//Create a new PendingIntent and add it to the AlarmManager
Intent intent3 = new Intent(context, ViewTasksActivity.class);
intent3.putExtra("goalid", Integer.toString(goal_id));
intent3.putExtra("activityId", Integer.toString(act.getActId()));
intent3.putExtra("activityName", act.getActivityName());
intent3.putExtra("show", "true");
PendingIntent pendingIntent = PendingIntent.getActivity(context,
act.getActId(), intent3, PendingIntent.FLAG_CANCEL_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
pendingIntent);
}
}
public void CancelAlarms(Context context, int goal_id)
{
dbhandler = new MyDBAdapter(context);
List<Activities> allact = dbhandler.getAllActivitiesByGoal(goal_id);
for (final Activities act : allact) {
Intent intent = new Intent(context, ViewTasksActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,
act.getActId(), intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);
}
}
}
感谢您的帮助。 :)