Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

android - How do I get my app to send a notification when it is closed?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...