0

I know how to create scheduled notification in Android by using alarm service but what I want to do now is to create notification in more frequent way.

For example, I want to push notification for 8 hours at the interval of 20 mins. In this case, is it efficient to use alarm service or timertask will be the better choice?

No matter which method, I wish to able to cancel it in the halfway. Thanks.

Sam
  • 1,252
  • 5
  • 20
  • 43

1 Answers1

0

Timertask starts new thread and works in it. So if your app will work in background and your app will be closed by android, you won't receive any notification. AlarmManager provides access to the system alarm services. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. (link). So it will start your app even if it was closed. And you have to understand how you app will work with this notifications. If it works only while user works in app, you can use timertask, but if it has to work in background(for example you will receive notification even if user doesn't work with a phone/tablet), it will be better use alarmanager. Hope it helps.

Dima
  • 1,189
  • 1
  • 13
  • 31
  • Oic. If let's say i want to set alarm for 1 hour with interval of 5 min, can i set it with only one alarm manager? and how about cancel it? Can i cancel it at the same time? – Sam Apr 11 '16 at 11:18
  • Cancel alarmmanager http://stackoverflow.com/a/3330782/2004305 And If you need to receive each 5 min a notification you can use setRepeating to describe repeating interval to receive notification http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int, long, long, android.app.PendingIntent) alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, FIVE_MINUTES, FIVE_MINUTES, alarmIntent); – Dima Apr 11 '16 at 12:07