ddnith
ddnith

Reputation: 149

Android Service running continuously

As per android documentation about Service LifeCycle http://developer.android.com/reference/android/app/Service.html#ServiceLifecycle

one you start the service "The service will at this point continue running until Context.stopService() or stopSelf() is called."

what is meant by service will continue running? If service is bound to some connection I understand that it's waiting for callbacks from it's connection but what if service is started ? Once onStartCommand is executed what is the purpose of service left?

Upvotes: 0

Views: 156

Answers (2)

m0skit0
m0skit0

Reputation: 25873

what is meant by service will continue running?

The process will be kept running.

Once onStartCommand is executed what is the purpose of service left?

That depends on what you want to do with the service. You usually want a service because you need it to be "listening" to something asynchronously (meaning you don't know when this something will arrive). For example, WhatsApp has a service that checks the server for new messages. If you don't need to "listen" to anything, you don't want a service.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006819

what is meant by service will continue running?

From Android's standpoint, a running service makes your process more important, and makes it less likely that Android will terminate that process to free up system RAM for use by other processes.

Once onStartCommand is executed what is the purpose of service left?

The only reason to use a service is because you are doing some work in the background that may take longer than a second or so, and you want to increase the odds that the work will complete.

So, for example:

  • music players will use a started service, so they can continue playing music even though the user leaves the player UI and does something else with their device

  • if your app needs to download a significant file, and it is important to the user that the file get downloaded, you would use a started service to do that work, rather than just forking some thread or AsyncTask from an activity

  • if your app needs to do work periodically in the background, you will use a started service in conjunction with things like AlarmManager or JobScheduler

Upvotes: 2

Related Questions