多个 AlarmManager 触发不是一次触发
Multiple AlarmManager firing isn't firing at a time
当我同时设置多个 AlarmManager
时,它只触发第一个。
有一个 MessageReceiver class 扩展了 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 则没有。我如何确保所有 AlarmManager 都在触发?
我以前像这样卖过这期:
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 添加频道 ID>26
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 class 添加到您的清单文件中!
快乐代码!
只需使用唯一值作为请求代码即可帮助解决问题。因此,更改只会进入 PendingIntent,更新后的 PendingIntent 将如下所示:
Random random = new Random();
PendingIntent msgPending = PendingIntent.getBroadcast(context,
random.nextInt(10000),
msgIntent,
PendingIntent.FLAG_ONE_SHOT);
现在会触发多个警报。
当我同时设置多个 AlarmManager
时,它只触发第一个。
有一个 MessageReceiver class 扩展了 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 则没有。我如何确保所有 AlarmManager 都在触发?
我以前像这样卖过这期:
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 添加频道 ID>26
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 class 添加到您的清单文件中!
快乐代码!
只需使用唯一值作为请求代码即可帮助解决问题。因此,更改只会进入 PendingIntent,更新后的 PendingIntent 将如下所示:
Random random = new Random();
PendingIntent msgPending = PendingIntent.getBroadcast(context,
random.nextInt(10000),
msgIntent,
PendingIntent.FLAG_ONE_SHOT);
现在会触发多个警报。