应用程序启动时触发Android通知

编程入门 行业动态 更新时间:2024-10-19 15:38:17
本文介绍了应用程序启动时触发Android通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使我的每日通知服务正常运行,但遇到了问题.

I'm trying to get my Daily Notification Service working properly but I am stuck with an issue.

我应该只在将来的一天的特定时间 收到通知,但是我注意到了

I should receive a notification only at a specific time of the day in the future but I noticed that

if (time_that_i_set_for_notification < current_time_of_the_day) notification triggers at the boot of my app

这是错误的,因为在这种情况下,通知应该仅在第二天触发 ,而不是在我启动应用后立即触发.

That's wrong because in that condition, the notification should trigger only the next day, not in the instant I launch the app.

这是我要使事情正常进行的尝试:

Here's my attempt to get the things working:

我在MainActivity的onCreate()方法中调用此:

private void scheduleNotification(int hour, int minute){ Intent notificationIntent = new Intent(this, NotificationReceiver.class); notificationIntent.putExtra("notifId", notifId); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notifId, notificationIntent, 0); // Set the alarm to start at approximately at a time DatePicker datePicker = new DatePicker(this); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth()); if(calendar.getTime()pareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 12); calendar.set(Calendar.MINUTE, 12); AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); // With setInexactRepeating(), you have to use one of the AlarmManager interval // constants--in this case, AlarmManager.INTERVAL_DAY. alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); }

我认为添加if(calendar.getTime()pareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1);会达到所需的结果.

I thought that adding if(calendar.getTime()pareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1); I would've achieved the result I needed.

谢谢您,祝您愉快.

推荐答案

好吧,那里的检查有误,如果您进行更改,则应该能够解决您的问题.

Ok there's a wrong check there, if you change it, you should be able to solve your issue.

我们在这里:

Intent notificationIntent = new Intent(this, NotificationReceiver.class); notificationIntent.putExtra("notifId", notifId); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notifId, notificationIntent, 0); // Set the alarm to start at approximately at a time DatePicker datePicker = new DatePicker(this); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth()); //if(calendar.getTime()pareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 14); calendar.set(Calendar.MINUTE, 50); if (calendar.getTimeInMillis() < System.currentTimeMillis()) calendar.setTimeInMillis(calendar.getTimeInMillis() + AlarmManager.INTERVAL_DAY); AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); // With setInexactRepeating(), you have to use one of the AlarmManager interval // constants--in this case, AlarmManager.INTERVAL_DAY. alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

我评论了//if(calendar.getTime()pareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1); ,因为它是不必要的.

I commented //if(calendar.getTime()pareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1); because it should be unnecessary.

让我知道它是否有效.

更新:您应该彻底检查处理您的日常通知的服务是否消耗大量资源.高耗电量的电池应用程序会打扰您的用户.

UPDATE: You should totally check whether the Service that handles your daily notification is high consume or not. A High-draining battery app would bother your users.

更多推荐

应用程序启动时触发Android通知

本文发布于:2023-11-27 07:31:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637134.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:启动时   应用程序   通知   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!