gpt4 book ai didi

android - 如何在 OnReceive 中将多个 alarmManager 与不同的通知链接起来?

转载 作者:行者123 更新时间:2023-11-29 17:26:21 25 4
gpt4 key购买 nike

我想在到达特定的 alarmManager 时触发不同的通知。

例如,当我们到达某个 alarmManager 中存储的独立日期时,我想显示“independence day”,当我们到达另一个 AlarmManager 中存储的他的死亡日期时,我想显示“Steve jobs died”,等等上。

所以我的问题是如何将每个 alarmManager(cal 和 cal2)与不同的通知相关联?

我一直这样做到现在,

在主 Activity 中:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE,19);
cal.set(Calendar.MONTH,11);
cal.set(Calendar.YEAR,2015);
cal.set(Calendar.HOUR_OF_DAY, 21);
cal.set(Calendar.MINUTE, 42);
cal.set(Calendar.SECOND, 30);

Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.DATE,19);
cal2.set(Calendar.MONTH,11);
cal2.set(Calendar.YEAR,2015);
cal2.set(Calendar.HOUR_OF_DAY, 21);
cal2.set(Calendar.MINUTE, 43);
cal2.set(Calendar.SECOND, 10);


Intent alertIntent = new Intent(this, AlertReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
alarmManager.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), PendingIntent.getBroadcast(this, 2, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));

请注意,我设置了触发通知的日期并且它正在工作,onReceive 中的通知被触发了两次,第一次是在我到达“cal”时,第二次是在我到达“cal2”。但是正如我上面提到的,我需要在到达 cal2 时触发不同的通知

这是 OnReceive:

public class AlertReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

createNotification(context, "title1", "independence day", "event of today");
//here i want to link this notification with AlarmManager containing Cal as a date
createNotification(context, "title2", "steve jobs died", "event of today");
//here i want to link this notification with AlarmManager containing Cal2 as a date




}

public void createNotification(Context context, String msg, String msgText, String msgAlert) {
PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);

NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.not)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
//intent to fire when notification clicked on
mBuilder.setContentIntent(notificIntent);
//how the person will be notified
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
//cancel notification when clicked in the taskbar
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager= (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0,mBuilder.build());


}

应用下面提到的内容后:我做了这 4 个事件:

AlarmManager alarmManager = (AlarmManager)  getSystemService(Context.ALARM_SERVICE);
Intent alertIntent = new Intent(this, AlertReceiver.class);
alertIntent.putExtra("title", "event1");
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));

Intent alertIntent2 = new Intent(this, AlertReceiver.class);
alertIntent2.putExtra("title", "event2");
alarmManager.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), PendingIntent.getBroadcast(this, 2, alertIntent2, PendingIntent.FLAG_UPDATE_CURRENT));

Intent alertIntent3 = new Intent(this, AlertReceiver.class);
alertIntent3.putExtra("title", "event3");
alarmManager.set(AlarmManager.RTC_WAKEUP, cal3.getTimeInMillis(), PendingIntent.getBroadcast(this, 3, alertIntent3, PendingIntent.FLAG_UPDATE_CURRENT));

Intent alertIntent4 = new Intent(this, AlertReceiver.class);
alertIntent4.putExtra("title", "event4");
alarmManager.set(AlarmManager.RTC_WAKEUP, cal4.getTimeInMillis(), PendingIntent.getBroadcast(this, 4, alertIntent3, PendingIntent.FLAG_UPDATE_CURRENT));

OnReceive 中:

 createNotification(context,intent.getStringExtra("title"), "event", " event");

有时第一次运行应用程序所有事件都会出现,有时第三个出现两次,前两个不出现,有时只出现最后一个,所以我的代码有问题或困惑,有什么问题吗?

日历:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE,24);
cal.set(Calendar.MONTH,11);
cal.set(Calendar.YEAR,2015);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 55);
cal.set(Calendar.SECOND, 10);

Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.DATE,24);
cal2.set(Calendar.MONTH,11);
cal2.set(Calendar.YEAR,2015);
cal2.set(Calendar.HOUR_OF_DAY, 23);
cal2.set(Calendar.MINUTE, 55);
cal2.set(Calendar.SECOND, 20);

Calendar cal3 = Calendar.getInstance();
cal3.set(Calendar.DATE,24);
cal3.set(Calendar.MONTH, 11);
cal3.set(Calendar.YEAR,2015);
cal3.set(Calendar.HOUR_OF_DAY, 23);
cal3.set(Calendar.MINUTE, 55);
cal3.set(Calendar.SECOND, 30);

Calendar cal4 = Calendar.getInstance();
cal4.set(Calendar.DATE,24);
cal4.set(Calendar.MONTH, 11);
cal4.set(Calendar.YEAR,2015);
cal4.set(Calendar.HOUR_OF_DAY, 23);
cal4.set(Calendar.MINUTE, 55);
cal4.set(Calendar.SECOND, 40);

最佳答案

您可以在安排警报时将该信息与 Intent 一起传递。

Intent alertIntent = new Intent(this, AlertReceiver.class);
alertIntent.putExtra("Notification Key", 1);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));

然后在您的 onReceive 中检查值并显示基于该值的通知:

Integer notificationId  = intent.getIntExtra("Notification Key", -1);
if (notificationId == 1)
{
// Show indipendente day
}
else
{
// do something else
}

除了传递整数值,您还可以传递通知消息并直接显示它,而无需任何 if...else 条件。

传递字符串数据:

alertIntent.putExtra("Notification Key", "Independence day");

取回字符串:

String message = intent.getStringExtra("Notification Key");
// Calling the notification
createNotification(context, "title1", message, "event of today");

关于android - 如何在 OnReceive 中将多个 alarmManager 与不同的通知链接起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34422217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com