简体   繁体   English

工人与服务 android 应用程序。 长时间运行应用程序,永不停止

[英]Worker vs Service android application. Running app for long time, never stops

Android : java class → Android : java class →

Worker vs Service ?工人服务

What is the difference between Worker and Service?工人和服务有什么区别? Which is better to do something in background for a long time.长时间在后台做某事哪个更好。 Application never stops.应用程序永远不会停止。

Dear friends.亲爱的朋友们。

Android: java class → Android: java class →

Worker vs Service工人与服务

I made an android application and inherited the Service class and it checks a website every minute and if there is new information on this website, it sends me a notification.我做了一个 android 申请并继承了服务 class,它每分钟检查一个网站,如果这个网站上有新信息,它会向我发送通知。

Does anyone know about Worker and what is the difference between Worker and Service?有谁知道 Worker,Worker 和 Service 有什么区别? Which is better to do something in background for long time.长时间在后台做某事哪个更好。 I want to make an application, that it never stops.我想做一个应用程序,它永远不会停止。

Don't do this.不要这样做。 This is the wrong architecture for a mobile device.这是移动设备的错误架构。 You'd kill the battery by forcing the cellular radio to constantly be transmitting.您会通过强制蜂窝无线电不断传输来耗尽电池电量。 Instead, use push notifications from your server to the device when there's new information.相反,当有新信息时,使用从您的服务器到设备的推送通知。

As for workers vs services- everything in Android is written to prevent long term background processes.至于工人与服务——Android 中的所有内容都是为了防止长期后台进程而编写的。 Background services can last a max of 2 minutes after your app is no longer in the foreground.在您的应用程序不再位于前台后,后台服务最多可以持续 2 分钟。 A Foreground service lasts longer, but can still be killed for resources.前台服务持续时间更长,但仍可能因资源而被终止。 A worker is a better idea for long term work, but can't be done once a minute.工人是长期工作的更好主意,但不能一分钟完成一次。 And if the phone is in Doze mode (entered a minute or two after the user puts the phone screen off) you're limited to one short period of processing every 15 minutes anyway.而且,如果手机处于打瞌睡模式(在用户关闭手机屏幕后一两分钟进入),您无论如何只能每 15 分钟进行一次短时间的处理。

The right way to do this is push messaging.正确的方法是推送消息。 If you want the notificaion to be instant, use high priority messages.如果您希望即时通知,请使用高优先级消息。 This will be better on your server side as well, as N clients pinging every minute would cause a ton of traffic and cost you a ton in hosting fees.这在您的服务器端也会更好,因为每分钟 ping N 个客户端会导致大量流量并花费您大量的托管费用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM