问题描述
我有一个活动,可以监听udp广播并在达到特定阈值时触发警报.但是当我退出应用程序时,我不再收到通知.如何保持应用程序在后台运行,以便在超过阈值时发送通知?我已经阅读了一些有关服务的内容,但不确定在活动和服务之间的关系中逻辑如何工作.服务应始终运行吗,应在应用程序关闭时启动服务吗?我希望该应用程序即使正在运行另一个应用程序也会触发警报.
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.
推荐答案
当用户要求继续进行此监视时,您的活动应启动一项服务.该服务将是具有套接字和用于监视UDP广播的线程的服务.您的活动还应该具有让用户停止和停止监视的功能,只要用户不再需要它.
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.
应用程序无法在Android中关闭".
Apps do not "close" in Android.
这篇关于关闭后,如何让我的应用发送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!