Skip to content

Android services Interview Questions & Answer

  • by

Android services are the most favorite and important interview questions for the Android developer (2-5 years experienced). Here we are covering the most important and frequently asked questions in an android interview.

Android services Interview Questions & Answer new

Q 1.  What are Android services

Ans: Android service is a component of an application that performs the long-running operation in android. Service also not providing a user interface (UI) and another application component can also interact with your service. It is not bound to the lifecycle of an activity.

Caution: A service runs in the main thread of its hosting process; the service does not create its own thread and does not run in a separate process unless you specify otherwise. If your service is going to perform any CPU-intensive work or blocking operations, such as MP3 playback or networking, you should create a new thread within the service to complete that work. By using a separate thread, you can reduce the risk of Application Not Responding (ANR) errors, and the application’s main thread can remain dedicated to user interaction with your activities.


Q 2. Type of services in Android

Ans: In Android, there is 3 type of services

Foreground: Foreground services are noticeable to the user like Play music, downloading, etc. Foreground services must display a Notification. It continues running even user not interacting with the application. A foreground service has the same priority as an active activity and should not be killed by the Android system, even if the system is low on memory. The process lies on onPause() and onResume()…example you play music player and pressing pause and play.

Background: By default services are background, meaning that if the system needs to kill them to reclaim more memory, they can be killed without too much effect. Example receiving the message, incoming call, receiving emails, setting alarms..the method used here is onStart() and onStop() for example:- check it on your phone..create an alarm at 6: 30 am..when system clock reaches the 6:30 am it order to kill the alarm service.

Bound: A service is bound when an application component binds to it by calling bindService(). Abound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC).

When the last client unbinds from the service, the system destroys the service EXCEPT If the service was started by startService

Q3. Is Android Services stops when the app is closed?

Ans: When the app is closed the service gets closed also because they are in one thread (main thread), so the service should be on another thread in order for it not to be closed.

First where the service does not have a foreground notification. In this case, your process is killed along with your service.

Second where the service has a foreground notification. In this case, the service is not killed and neither is the process

The third scenario If the service does not have a foreground notification, it can still keep running if the app is closed. We can do this by making the service run in a different process.

You can create a service in a separate process by including the below attribute in your manifest.
android:process=”yourService” process name must begin with lower case.

<service android:name=".Service2"

Declare this in your manifest. Give a custom name to your process and make that process isolated and exported.

4. Is an android service run in the background?

A Service is an Android application component without a UI that runs on the main thread (of the hosting process). It also has to be declared in the AndroidManifest.xml. If you want the service code to run in a Background Thread, then you must manage that yourself.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading