Home > Enterprise >  Worker vs Service android application. Running app for long time, never stops
Worker vs Service android application. Running app for long time, never stops

Time:12-05

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 →

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.

Does anyone know about Worker and what is the difference between Worker and Service? Which is better to do something in background for long time. I want to make an application, that it never stops.

CodePudding user response:

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. Background services can last a max of 2 minutes after your app is no longer in the foreground. 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.

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.

  • Related