By Default, a service runs on the main thread. So if you want to do some long running task you need to make your service run on the background thread.
There are basically three types of service -
- Bound
- Foreground
- Background
To create a service you need to create a subclass for service or use any existing subclass for the service. It is important to override some of the important callback methods of service class in order to run a service properly. There are some callback methods which need to be implemented such as-
- onStartCommand
- onBind
- onCreate
- onDestroy
In manifest, you need to declare your service -
<manifest ... >
...
<application ... >
<service android:name=".ExampleService" />
...
</application>
</manifest>