当我同时设置多个AlarmManager
时,它只会触发第一个。
有一个扩展 BroadcastReceiver 的 MessageReceiver 类。当 AlarmManager 将触发 MessageReceiver (onReceive(( 方法(时将被执行。 从 MainActivity 中,我创建了 PendingIntent 并设置了 AlarmManager。
消息接收器.java
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Msg","triggered");
//Do some work
}
}
主活动.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 没有。如何确保所有警报管理器都在触发?
只需使用唯一值作为请求代码即可帮助解决问题。因此,更改将仅进入待定意图,更新后的待定意图将如下所示:
Random random = new Random();
PendingIntent msgPending = PendingIntent.getBroadcast(context,
random.nextInt(10000),
msgIntent,
PendingIntent.FLAG_ONE_SHOT);
现在将触发多个警报。
我之前像这样卖过这个问题:
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);
}
}
}
}
如您所知,随机是每个警报的更改请求代码。通过这样做,我们得到了所有不同的警报。
注意:请将我的应用程序类添加到您的清单文件中!