当我同时设置多个AlarmManager
时,它只会触发第一个。
有一个MessageReceiver类,它扩展了BroadcastReceiver。当AlarmManager将触发MessageReceiver时(将执行onReceive()方法)。 在MainActivity中,我创建PendingIntent并设置AlarmManager。
MessageReceiver.java
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Msg","triggered");
//Do some work
}
}
MainActivity.java
AlarmManager msgAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent msgIntent = new Intent(MainActivity.this,MessageReceiver.class);
PendingIntent msgPending = PendingIntent.getBroadcast(context,
++requestCode,
msgIntent,
PendingIntent.FLAG_ONE_SHOT);//requestCode is a global variable
msgAlarmManager.setExact(
AlarmManager.RTC_WAKEUP,
calender.getTimeInMillis(),
msgPending); //calender is a Calender Object
MainActivity.java中的这段代码将在我的应用程序的单个运行时中多次执行。
我希望每个警报都被触发,即使它们应该同时被触发。但就我而言,唯一的第一个是触发,另一个不是。如何确保所有AlarmManager都在触发?
答案 0 :(得分:0)
我以前这样卖过这个问题:
private void createReminder(Activity activity, Notification notification, MyModelClass model) {
final int min = 1;
final int max = 9999;
final int random = new Random().nextInt((max - min) + 1) + min;
Intent notificationIntent = new Intent(activity, AlarmReceiver.class);
notificationIntent.putExtra(AlarmReceiver.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, random, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long delay = model.dateAndTime.getTime() - Calendar.getInstance().getTimeInMillis();
long futureInMillis = SystemClock.elapsedRealtime() + delay;
AlarmManager alarmManager = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null && model.isAlarmOn) {
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}
}
private Notification getNotification(Activity activity, MyModelClass model) {
PendingIntent newEntryActivityPendingIntent = PendingIntent.getActivity(activity, 1, new Intent(activity, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(activity, Common.CHANNEL_ID)
.setContentTitle("Content title")
.setContentText("ContentText")
.setTicker("Hey Come on !")
.setSmallIcon(R.drawable.ic_launcher_background)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setContentIntent(newEntryActivityPendingIntent);
return builder.build();
}
我的接收者是这样;
public class AlarmReceiver extends BroadcastReceiver {
public static final String NOTIFICATION = "notification";
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
notificationManager.notify(1, notification);
}
}
不要忘记为sdk> 26添加频道ID
public class MyApplication extends MultiDexApplication {
public static final String CHANNEL_ID = "reminderNotificationChannel"
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Reminder Notification Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
if (manager != null) {
manager.createNotificationChannel(serviceChannel);
}
}
}
}
您知道,随机是每个警报的更改请求代码。这样,我们得到了所有不同的警报。
注意:请将MyApplication类添加到清单文件中!
快乐代码!
答案 1 :(得分:0)
仅使用唯一值作为请求代码有助于解决问题。因此,更改将仅在PendingIntent中进行,而更新的PendingIntent将如下所示:
Random random = new Random();
PendingIntent msgPending = PendingIntent.getBroadcast(context,
random.nextInt(10000),
msgIntent,
PendingIntent.FLAG_ONE_SHOT);
现在将触发多个警报。