每天创建通知 [英] Create Notification each day

查看:29
本文介绍了每天创建通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a Notification each Day at 8:00am. I have some data in a SQLite database and every day at this time I want to get the data from it and create a notification from it. The creation of a new notification is no problem but how can I display it every day at this time?

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

I think I have to work with a Service but how can I tell the system to start this service at the special moment? And what kind of Service should I use? I think if the system calls the service it starts a specific function where I can run my code to connect to the database and create and send my notification to the system right?

我不明白的是,如果我在主 Activity 中注册了该服务,为什么如果用户关闭我的应用程序,系统还能启动该服务?谁能给我解释一下?我一直认为如果我的主要 Activity 被破坏,服务也会被破坏.

What I can't understand is if I register the service in my main Activity why can the system start the service if the user close my app? Can anyone explain that to me? I allways think if my main Activity is destroyed the service is destroyed, too.

推荐答案

使用 警报管理器 类并将通知放在NotifyService 类中.这将在每天早上 8 点设置闹钟:

Use the Alarm manager class and put the notification in a NotifyService class. This will set an alarm at 8am everyday:

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

这篇关于每天创建通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆