Services are one of the important application components in Android that helps to execute long running operations in background. In fact what I thought was, Services would execute as a background thread without interrupting the main thread. But soon I found that my assumption was wrong, in fact Service also run in the main thread of the application where the UI operations occur. Therefore, if we perform a long task in onStartCommand() method, it will block the main thread the end result is, unresponsive application . So to avoid the problem, you have to execute the complex task into a separate thread. this can be achieved with the help of
- Declaring seperate Thread
Additionally there two memory management techniques as well. Declaring the return statement in onStartCommand() method.
START_STICKY: Notifies the OS to recreate the service after it has enough memory and call onStartCommand() again with a null intent.
START_NOT_STICKY: Notifies the OS not to recreating the service again.