问题 每天创建通知


我想在每天上午8:00创建通知。我在SQLite数据库中有一些数据,此时我每天都想从中获取数据并从中创建通知。创建新通知没有问题,但我现在每天如何显示它?

我想我必须使用服务但是如何告诉系统在特殊时刻启动此服务?我应该使用什么样的服务?我想如果系统调用该服务,它会启动一个特定的功能,我可以运行我的代码连接到数据库并创建并将我的通知发送到系统吗?

我无法理解的是,如果我在我的主Activity中注册该服务,为什么系统会在用户关闭我的应用程序时启动服务?任何人都可以向我解释一下吗?我总是认为如果我的主要活动被破坏,服务也会被破坏。


11054
2018-05-10 16:48


起源

使用alarmManager。 - wtsang02
stackoverflow.com/questions/4430849/... - Rachel Gallen
mobile.tutsplus.com/tutorials/android/... - Rachel Gallen


答案:


使用 报警管理器 上课并将通知放入 NotifyService 类。这将在每天早上8点发出警报:

Intent myIntent = new Intent(Current.this , NotifyService.class);     
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 08);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);  //set repeating every 24 hours

7
2018-05-10 16:53



我有相同的设置,我每小时都会收到通知,而不是每天一次。 - Roon13
这将一直有效,直到用户重新启动他的手机。要在重启后保持AlarmManager正常工作,请查看此帖子 stackoverflow.com/questions/12512717/... - Vikram Singh
同样在这里...它不是每天发送一次,而是每次我打开应用程序 - Si8
可以使用预定义的AlarmManager.INTERVAL_DAY代替24 * 60 * 60 * 1000,减少出错的几率...... - ked


您不需要服务器。我认为实现这个的最好方法是使用AlarmManager。

报警管理器示例


2
2018-05-10 16:53



拼写错误将AlertManager更改为AlarmManager - Dheeraj Bhaskar