I am writing an android application, which communicates with server when the user logs in to the application. Now if the user closes the application without logging out, I want to query the server every 15 minutes, to see whether the particular user has received any updates. If yes then, I want to push a notification, on clicking which the user goes straight into the applications activity which shows the update.
How can this be implemented in android? Is it possible?
Can anyone suggest solutions using timer? And please remember this background program should run only when the actual application is closed.

- 12,691
- 31
- 118
- 190
2 Answers
You can use Services for this purpose. Take look at this: http://developer.android.com/guide/topics/fundamentals/services.html http://marakana.com/forums/android/examples/60.html http://developer.android.com/guide/topics/ui/notifiers/notifications.html

- 1,031
- 11
- 25
-
Will service run even when the application itself is closed? – Ashwin Mar 25 '12 at 13:01
-
Yes. Services are usually used for this kind of long running operation in background. If you stop your application your service will continue to work. Service can stop itself or can be stopped by the system or by application that started that service. – Emran Mar 25 '12 at 14:09
Yes, this is possible.
I would do the following:
Use
AlarmManager
withsetRepeating
. This will get you your 15 minute interval.In
setRepeating
, pass in aPendingIntent
for anIntentService
subclassIn your
IntentService
subclass, inhandleIntent
, query your server then create aNotification
like documented at http://developer.android.com/guide/topics/ui/notifiers/notifications.htmlThe
Notification
will contain anotherPendingIntent
which will bring the user back to your app. Make sure to specify theActivity
that contains the UI that is relevant for that update.
You can learn more about IntentServices in the Services Guide at http://developer.android.com/guide/topics/fundamentals/services.html
You can learn more about AlarmManager at http://developer.android.com/reference/android/app/AlarmManager.html

- 14,881
- 3
- 26
- 31
-
-
`AlarmManager` will start your `IntentService` even if there are no visible Activities. This is documented well at http://developer.android.com/reference/android/app/AlarmManager.html > Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. – louielouie Mar 25 '12 at 22:39
-
thank you for the answer. Can you also please point to some tutorial or some example which describes the use of an alarm manager to invoke an intent service? – Ashwin Apr 01 '12 at 06:20
-
You're welcome. One place where you can find some code that illustrates this is the answer to http://stackoverflow.com/questions/3859489/android-running-a-background-task-using-alarmmanager – louielouie Apr 01 '12 at 06:26
-
Please bear with me. I have one more question for you. Why do you suggest I pass a pending intent for an Intent Service? The same thing can be done in the onreceive() function of the broadcast receiver called by the alarmmanager. What is the advantage with using a service(Intent Service)? – Ashwin Apr 01 '12 at 17:06