android应用程序关闭后使用侦听器 [英] Using a listener after an android application is closed

查看:105
本文介绍了android应用程序关闭后使用侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关向我的应用程序中添加服务的信息,并且自7/8开始,现在存在服务限制以提高电话性能.

I've just been reading about adding a service to my application, and since 7/8 there are now service restrictions to improve phone performance.

我已经看到推荐的方法是使用作业调度程序,但是如果我这样做的话,那不仅会定期启动新的侦听器吗?

I've seen that the recommended approach is to use a job scheduler, but will that not just periodically start a new listener if I did that?

基本上,我更新了数据库,并希望使用快照侦听器实时更新用户.关闭应用程序后,我想发送通知.

Basically I update my database, and using a snapshot listener I want to update my user in real time. When the app is closed, I'd like to send a notification.

我的问题(如果我是对的话)是,不断提出新的Firestore请求会耗尽我的请求限额.

My issues (if I'm correct) are that constantly making a new Firestore request will eat through my request allowance.

还,如果它是一个工作计划程序,那将不是实时的吗?

Also, if its a job scheduler it won't quite be real time?

我已经读到您可以使用前台服务,但这似乎并不需要真正的任务,它需要永久性的通知,并且会惹恼用户.

I've read that you can use a foreground service, but this doesn't really seem like that task that needs a permanent notification and would annoy the user.

任何人都知道如何实现此目的的提示?

Anyone got any tips on how I'd implement this?

谢谢

推荐答案

在关闭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.

这篇关于android应用程序关闭后使用侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆