Explain the concept of Services in Android.

Instruction: Discuss what Services are used for in Android and the difference between started and bound Services.

Context: This question evaluates the candidate's understanding of Services in Android, including their lifecycle and how they are used to perform background operations.

Updated

Official answer

A Service is an Android component without its own screen. It may expose ongoing work or an IPC interface, but it is neither a background thread nor a guarantee that the process will remain alive.

A started service receives onStartCommand after an allowed start. It remains logically started until stopped with stopSelf or stopService, subject to system and user restrictions. A bound service exposes an IBinder interface through onBind. If it is only bound, the final unbind permits destruction. A service can be both started and bound: removing the started state does not destroy it while clients remain bound, and unbinding alone does not end its started state.

Callbacks ordinarily run on the hosting process's main thread. Move blocking work to an appropriate executor or coroutine dispatcher, protect shared state and release bindings/resources at their owner's lifecycle boundary. onDestroy is not guaranteed when the process is killed.

For user-visible ongoing playback, use the appropriate media/foreground-service design with required notification, types and permissions. Background starts and execution have OS/target-specific restrictions. For persistent deferrable synchronization, consider WorkManager; for a screen-only operation, a scoped coroutine may suffice.

Test start-only, bind-only and combined ownership, client disconnection and process restart. A restart mode is a recovery policy, not permission to run indefinitely or proof that a side effect executes exactly once.

Service execution, Foreground service limits

Related Questions