So I want my app to send a notification at 8:00 a.m. everyday subjected to whether there is an event on that day or not.
I have a SQLlite DB which stores the dates on which notification is to be sent. What I want from my app, is this- Everyday at 8 in the morning, it should check if there is an event for today. And if there is an event for that day, then send a notification.
I have already implemented the part with the DB and the notification. I just need to implement the part where the app checks the DB at 8 a.m. everyday.
**Update: ** I made a service which checks if the current time is 8:00 or not. Following is the code for that service.
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
System.out.println("inside on start command for service");
myPrefs = this.getSharedPreferences("settings", this.MODE_PRIVATE);
checkTime();
return START_STICKY;
}
// to check if the time for alarm is here
private void checkTime() {
// TODO Auto-generated method stub
try{
System.out.println("inside check time");
Calendar cal;
cal = Calendar.getInstance();
if (08 == cal.get(cal.HOUR_OF_DAY) && 00 == cal.get(cal.MINUTE)) {
// to call db and check for events
nm = new MyNotificationManager(this);
if (nm.checkEvent()) {
nm.setNotifications();
}
}
}catch(Exception e){
System.out.println("inside exception for check time "+e.toString());
}
}
The problem is that the service is only checking the time once. How to make it check the time every minute? Can anyone please help me?
Use Pending Intent
A pending intent is a token that you give to another application (e.g., notification manager, alarm manager or other 3rd party applications), which allows this other application to use the permissions of your application to execute a predefined piece of code.
To perform a broadcast via a pending intent, get a PendingIntent via the getBroadcast() method of the PendingIntent class. To perform an activity via a pending intent, you receive the activity via PendingIntent.getActivity().
Or
PendingIntent + Alarm Manager + Broadcast Receiver combination can work fine you have to register and unregistere broadcast receiver dynamically not in manifest. Like