I have an activity that listens to a udp broadcast and triggers an alarm if a certain threshold is reached. But when I exit the app, I no longer receive the notification. How do i keep the app running in the background so that it will send a notification when the threshold is crossed? I have read a bit on services but I am not sure how the logic would work in a relationship between an activity and service. Should the service always be running, should the service start when the app closes? I want the app to trigger an alarm even if another app is running.
Asked
Active
Viewed 2,429 times
-2
-
Alarms is a mechanism for sending intents at some point or points in the future. This allows one application to make code execute, even when that application is no longer running. Once alarms are registered they remain active even if the device is asleep.You can even configure alarms to wake a sleeping device. Alarms are canceled on device shutdown/restart. To give you some examples of alarm. For instance the MMS messaging application uses alarms to start a service that can find MMS messages that have not been delivered and try to deliver them. – android_Muncher Jun 02 '14 at 20:18
1 Answers
1
How do i keep the app running in the background so that it will send a notification when the threshold is crossed?
Your activity should start a service, when the user asks for this monitoring to go on. That service would be the one that has the socket and the thread for monitoring for UDP broadcasts. Your activity should also have the means for the user to stop this monitoring, if and when the user no longer wants it.
Bear in mind that your service will not run forever. It can go away at any time, either due to user action or an OS decision. However, it will be available for far longer than will your activity.
Should the service always be running, should the service start when the app closes?
Apps do not "close" in Android.

CommonsWare
- 986,068
- 189
- 2,389
- 2,491
-
Thank you very much, so the activity should have the gui of stopping and starting the interface, and the service should monitor? And if the threshold is crossed, the service will trigger the alarm? Thank you for the help. – user3700692 Jun 02 '14 at 20:20
-
@user3700692: "so the activity should have the gui of stopping and starting the interface, and the service should monitor? And if the threshold is crossed, the service will trigger the alarm? " -- yes. – CommonsWare Jun 02 '14 at 20:29