Ashwin
Ashwin

Reputation: 13537

writing a background program that executes when the main application is closed in android

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.

Upvotes: 1

Views: 301

Answers (2)

louielouie
louielouie

Reputation: 14941

Yes, this is possible.

I would do the following:

  1. Use AlarmManager with setRepeating. This will get you your 15 minute interval.

  2. In setRepeating, pass in a PendingIntent for an IntentService subclass

  3. In your IntentService subclass, in handleIntent, query your server then create a Notification like documented at http://developer.android.com/guide/topics/ui/notifiers/notifications.html

  4. The Notification will contain another PendingIntent which will bring the user back to your app. Make sure to specify the Activity 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

Upvotes: 1

Emran
Emran

Reputation: 1031

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

Upvotes: 2

Related Questions