且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

android应用程序关闭后使用侦听器

更新时间:2023-01-12 09:26:52

在关闭android应用程序后使用侦听器

Using a listener after an android application is closed

您可以在关闭Android应用程序后使用监听器,方法是不将其删除.使用侦听器后,您还需要 删除它 (根据您的活动的生命周期).但这只能在短时间内起作用,因为如果应用程序不在前台,Android将会停止您的服务.这样做是为了在不使用应用程序时节省资源.它还可能会阻止您的应用程序进行任何联网,甚至完全终止应用程序进程.正如您已经提到的,除了使它成为前台服务之外,您无法采取任何措施来阻止这种情况.

You can use a listener after an android application is closed, by not removing it. Once you are using a listener, you also need to remove it according to the life-cycle of your activity. But this will work only for a shot period of time because Android will stop your service if the app is not in the foreground. It does this to save resources when the app isn't being used. It also might stop your app from doing any networking, or even kill the app process completely. There's nothing you can do to prevent this, other than making it a foreground service, as you already mentioned.

对于您的情况,前台服务可能不是***的选择,对于您的用户也不是***的选择.详细了解后台服务的限制.

A foreground service is probably not the best thing to do for your case, nor is it the best thing for your users. Read more about limitations on background services.

我的建议是使用 Firebase Cloud Messaging 在出现问题时通知您的应用已经更改了它可能感兴趣的位置.因此,即使用户保持关闭其应用程序的状态,您的用户也会收到通知.

My recommendation is to use Firebase Cloud Messaging to notify your app when something has changed that it might be interested in. So your users will recieve notification even if they will keep their app closed.