Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Notification each day

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?

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.

like image 732
Cilenco Avatar asked May 10 '13 16:05

Cilenco


1 Answers

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
like image 197
Buneme Kyakilika Avatar answered Oct 10 '22 23:10

Buneme Kyakilika