我正在尝试在 BOOT_COMPLETED 上从数据库中加载一些任务并为每个任务设置警报。警报管理器配置为接收 AndroidMANifest 文件中的 BOOT_COMPLETED。
有时我通过短信获得这些任务,所以我有一个用于短信接收和处理的 brodcastreceiver,它构建任务列表并调用 AlarmManager.setAlarms()。
我想知道当从 OnReceive() 方法 og SMSReceiver 调用时 setAlarams 工作正常,但在 Boot_Completed 上从 AlarmManager 的 OnReceive() 方法调用时不能正常工作。它只设置一个警报并忽略列表的其余部分!
有什么帮助吗?
提前致谢
public class AlarmManager extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
List <Task> taskList= db.loadFromDB();
setAlarms(context, taskList);
}
public static void setAlarms(Context context, List<Task> taskList) {
for each task in taskList{
int pendingIntentRequestCode = task.getid();;
Intent myIntent = new Intent(context, AlarmReceiver.class);
myIntent.putExtra("taskName", task.getName());
myIntent.putExtra("taskHour", task.getHour));
myIntent.putExtra("taskMinute", task.getMinute());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
pendingIntentRequestCode,
myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,alarmTime, pendingIntent );
}
}
}
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String taskName = intent.getStringExtra("taskName");
int taskHour = intent.getIntExtra("taskHour", -1);
int taskMinute = intent.getIntExtra("taskMinute", -1);
Intent alarmIntent = new Intent(AlarmClock.ACTION_SET_ALARM);
alarmIntent.putExtra(AlarmClock.EXTRA_MESSAGE, taskName);
alarmIntent.putExtra(AlarmClock.EXTRA_HOUR, ataskHour);
alarmIntent.putExtra(AlarmClock.EXTRA_MINUTES, taskMinute);
alarmIntent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alarmIntent);
}
}
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String messageReceived = "";
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
messageReceived += msgs[i].getMessageBody().toString();
sender = msgs[i].getOriginatingAddress();
messageReceived += "\n";
}
List <Task> taskList = MakeTaskListFromReceivedSMS(messageReceived);
AlarmManager.setAlarms(context, taskList);
}
}
}