- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在到达特定的 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/
我想在每次触发 AlarmManager 时额外更改 Intent。这可能吗,我如何在 AlarmManager 触发后立即调用它? 代码: public void startCollector(){
public class AlarmManagerClass extends BroadcastReceiver { public static boolean haveInternet
我正在寻找执行简单任务的最有效方法。作为一名新的 android 开发人员,我不太确定这些策略中的哪一个在内存效率方面最适合我的应用程序。我想其中一些方法可能会导致我不知道的线程问题。 所有三个解决方
我的应用程序有一个实时模块,它应该每 60 秒 ping 服务器一次。否则,连接将断开,设备将需要重新连接。 第一次尝试是使用 Thread.Sleep 使 ping 线程以所需的频率运行。第二次尝试
我有一组在重启后需要保留的警报。我试过在启动接收器上使用,但它们不会重新启动。我不确定我是否了解启动接收器以及如何重新启动所有警报。我已经有一个接收器来接收我的通知,但不知道我是否可以使用同一个接收器
我有一个要在其中创建警报的 Android 应用程序。这是一个正常的闹钟,应该在指定的时间叫醒你。与任何警报一样,您还可以通过在创建警报时选择声音文件来指定它将使用哪种警报声音。 闹钟效果很好,它在正
我的任务是定期读取后端的手机传感器(例如 WiFi、加速度计)。 我目前的解决方案是使用 AlarmManager。 具体来说,我们有: 在“主”程序(一个 Activity )中,我们使用 Pend
public void SetAlarm(Context context, int sec) { AlarmManager am=(AlarmManager)context.getSyst
我已经通过警报管理器制作了简单的警报,问题是当插入很多秒时,它无法及时工作,这是我在这里声明的 这是代码 mo = ((Integer.parseInt(mons.getText().toStrin
我在应用程序中设置了一个闹钟,每 X 分钟触发一次通知。第一次有效,但随后就不重复了。 我正在开发的 API 是 API 18 (Android 4.3)。 MainActivity.class pu
我很难专心于对 AlarmManager 进行编程。 下面的代码几乎只是将当前日期分配给文本字段并获取一些 XML 并使用 setListAdapter 显示它。我希望这段代码在启动时运行,然后每 3
我对如何停止 AlarmManager 有疑问。我想在“x”小时开始并在“y”小时停止,由用户设置。我知道“.cancel”功能的存在,但不知道如何设置“y”小时内的停止时间。 创建时 final E
我的应用程序有一个 AlarmManager 设置为每 60 秒触发一次。它会触发一个 fragment ,该 fragment 检查当前时间并查找应用程序日历中当时是否有任何事件发生。它运行得很好,
我正在尝试安排使用 AlarmManager 调用的方法,但它似乎不起作用。我看过其他例子,但他们的例子不适合我。所以我认为这是我的代码中的一些内容。这是 AlarmManager 代码: Alarm
我将 AlarmManager 设置如下: Intent startIntent = new Intent(context, MyService.class); PendingIntent pend
我正在尝试使用警报管理器发出警报。应用程序运行时没有任何错误消息,但没有任何反应。 我尝试了来自developer.android.com的解决方案和来自stackoverflow的建议。我也尝试复制
public class background_alarm extends BroadcastReceiver { @Override public void onReceive(Context co
我编写了一个每天凌晨 5:22 运行的闹钟代码。该代码在第一个时间间隔内运行良好,但在第二个时间间隔内会在 24 小时之前触发。 我在 MainActivity 的 onCreate() 方法中添加了
我在这些类(class)中有两个类(class),A,B。他们两个我都有一个像下一个这样的警报管理器; class A{ PendingIntent sender; AlarmManager am
我在 9:00 和 21:00 有两个 AlarmManager,但只运行第二个闹钟。 ////////9:00am///////// Intent aviso = new Inten
我是一名优秀的程序员,十分优秀!