In my project, I have a database with Year, Month, Day, Hour, Minute. For each row in this database I want to put notification with Title and description for user in time, that described in that row, but when I use NotificationManager, it activates at once when I add new time to database. I read this article: Alarm Manager Example, about Alarm Manager Example, but I still can't understand, how to use it for notifications, because when I try to use, nothing happens. I'll be glad, if someone can help me.
1 Answers
Your message isn't clear to me. If you are trying to launch notifications at a certain time, this is one way to do it. Use 2 services; one service (you could call it SetAlarmService) to read your DB and set a pending intent to launch at a certain time with the AlarmManager. You can get an instance by calling getSystemService(Context.ALARM_SERVICE);. You should set your pending intent to launch another service (you could call it NotifyService), which will simply put up the notification as soon as it is started.
EDIT: here is a quick example, see the documentation for explanations of parameters, etc.
public class AlarmService extends Service {
Time time;
AlarmManager alarmMan;
@Override
public void onCreate() {
alarmMan = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
time = new Time();
}
@Override
public int onStartCommand(Intent intent, int startID, int flags) {
time.setToNow();
alarmMan.set(AlarmManager.RTC_WAKEUP, time.toMillis(false)+(10*1000), getPIntent());
time = null;
}
public PendingIntent getPIntent() {
Intent startIntent = new Intent(this, NotifyService.class);
startIntent.setAction(com.berrmal.remindme.NotifyService.ACTION_SEND_NOTIFICATION);
PendingIntent pIntent = PendingIntent.getService(this, 0, startIntent, PendingIntent.FLAG_CANCEL_CURRENT);
return pIntent;
}
I launch this service from an activity, you can do it any way you want. NotifyService.class is another service I have written that just immediately posts a sticky notification, I'm not going to show that because it sounds like you already know how to use the NotificationManager. The key here is the 10*1000, that is how many milliseconds in the future the alarm will be activated, and thus what time the notification will show up. You could read that from a file, etc. In this example, I am just calculating 10000 millis in the future from now. The RTC_WAKEUP flag is one of 4 flags that you will want to read about, they make the alarm do slightly different things. Hope that helps.

- 744
- 2
- 14
- 29
-
I tried this too, with no result. Maybe I did something wrong, can you give me an example please? – Askar Zaitov Sep 09 '13 at 21:53