1

What is the difference between service and a thread?

  • Service - is a component of android, which runs in the background with out any UI. Service will have default one thread to run in the back ground. Thread - is similar to service, it also runs in the background
  • Service - is a component of android, which runs in the background with out any UI. By default service will run in Main thread only. Thread - is not android component, but still one can use thread to do some background task. Using thread in place of service is discouraged.
  • Option 2 is right, but one can use Thread in place of Service, no problem will come.
  • Option 1 is right, but it may have UI also some times.

Answer: B Service - is a component of android, which runs in the background with out any UI. By default service will run in Main thread only. Thread - is not android component, but still one can use thread to do some background task. Using thread in place of service is discouraged.
Description: Service : is a component of android which performs long running operation in the background, mostly with out having UI.
Thread : is a O.S level feature that allow you to do some operation in the background.

Though conceptually both looks similar there are some crucial differentiation.

1.Service - if it is destroyed while performing its job, in the middle by Android due to low memory scenario. Then android will make sure that it will restart your service, if you have returned START_STICKY or START_REDELIVER_INTENT from onStartCommand().
2.Thread - if it is destroyed by android in middle due to low memory, then android will not guarantee to restart it again. That means user lost his half work.
3.Service - is a component of android, so it has priority levels to be considered while destroying an application due to low memory. Thread- is not a component of android, so android will not take thread priority into consideration while killing an application due to low memory.

I will try to explain this 3rd point.

Lets say you have a requirement of connecting to internet from your activity. You can do it by using a service(with thread) or directly by creating a thread in activity. Consider the second scenario where you are connecting to internet in a thread. Then

i. What will happen if user closes the activity, while still thread is running in the background. Will that thread continue to run in back ground ? Answer is you can't really predict.

ii.Assume that in continuation for above scenario, even after killing activity your thread continued to do its intended operation. Then there is a low memory situation arises in your phone. Then this application will be the first susceptible app to be killed as there is no priority for this application.

So bottom line is: If you want to do some heavy background functionality then it is always better to have a service with thread. If you feel that that background functionality to be alive as long as your activity is alive, then go for activity with thread or activity with async task.

2

start a service from activity and close activity, what will happen to that service, will it be alive or dead?

  • service will also be killed
  • service will be alive for some time, and will be killed by android garbage collector.
  • service will run for ever, no body can stop it now, and it leaks memory.
  • service will be keep running in the background, but it can stop itself when the work given to it is done. Or others also can kill that service using stopService(), or android also can kill the service forcefully in case of low memory scenarios.

Answer: D service will be keep running in the background, but it can stop itself when the work given to it is done. Or others also can kill that service using stopService(), or android also can kill the service forcefully in case of low memory scenarios.
Description: service will be keep running in the background, even if the activity which has created is no more alive.But it can stop itself when the work given to it is done. Or others also can kill that service using stopService(), or android also can kill the service forcefully in case of low memory scenarios.

3

How to stop a service in android?

  • stofSelf()
  • stopService()
  • android may also kill service forcefully in cases of low memory
  • all above options are rite

Answer: D all above options are rite
Description: One can stop a service using 1.stopSelf() or 2.stopService(). There are some situations where android may also kill running services in case if it requires memory and phone is running out of memory(low memory).

4

Bound service : binding to a service, what is the function that needs to be implemented in service class?

  • onCreate()
  • onBind() & onUnbind()
  • ServiceConnectionListener in client side.
  • all above 3 are required.

Answer: D all above 3 are required.
Description: to implement a binded service one has to implement onBind(), onUnBind() functions in service class and onServiceConnected() in client side class.

5

How to create a service with multiple threads in it?

  • Create a service with creating thread in onStartCommand
  • Use Intent Service
  • Create a service with one thread in OnCreate().
  • Either use option1 or use Async task with service

Answer: D Either use option1 or use Async task with service
Description: Create a service with creating thread in onStartCommand. Or simply use asynctask in the service.

6

How to update UI from a service that has threads?

  • Create a thread in the Service class and directly access UI components of your activity
  • Since updating UI from other thread directly is not possible, communicate with Main UI thread for the UI updates
  • Use Intent Service
  • Either use option1 or option 2

Answer: B Since updating UI from other thread directly is not possible, communicate with Main UI thread for the UI updates
Description: Android follows singled threaded UI model, i.e other threads should not touch UI with out taking permission of Main UI thread. If other threads wants to touch UI, communicate it to Main thread. Many ways are there to achieve it, use 1. RunOnUiThread() 2. use Handlers to communicate to main thread 3. Use AsyncTask and update ui from onPre or onPost or onProgress ..

7

How to start a service with foreground priority?

  • Service always runs in background since it doesn't have UI, so its not possible.
  • startService(intent, boolean foreground); pass second parameter true, to make this service as foreground.
  • Set priority to service in <service> tag in the manifest file. if priority is any thing other than 0, then it will be treated as foreground service.
  • startForeground (int id, Notification notification), use this function in onCreate() of your service.

Answer: D startForeground (int id, Notification notification), use this function in onCreate() of your service.
Description: Generally services will run in background, which is of 3rd priority. if you feel that the service is critical for user, then you can increase its priority by making it foreground service. Use function startForeground (int id, Notification notification), in onCreate() of your service to make this service as foreground service.
Foreground services will be treated with highest priority, so android will ensure it will not kill these services even in case of low memory situations. Eg: MP3 player service is a foreground service.

8

Service with threads: My application has only a service, and my service performs heavy lift functionality to connect to internet and fetch data, now should I create a thread or not? If so why?

  • Yes, because by default services with out any thread uses Main thread.
  • Option 1 is right, because if services and activities runs in only one thread, it may lead to ANR error.
  • No, you don't need to create a thread because a service will create a thread in the back ground when ever some one starts your service.
  • No need to create a new thread in Service as it is not required in this scenario. Because service runs in the main thread. Since our app doesn't have any activities, so its OK to run service in main thread.

Answer: D No need to create a new thread in Service as it is not required in this scenario. Because service runs in the main thread. Since our app doesn't have any activities, so its OK to run service in main thread.

Description: No need to create a new thread in Service as it is not required in this scenario. Because service runs in the main thread. Since our app doesn't have any activities, so its OK to run service in main thread. ANR error will occur only if activities and services runs in same thread.

9

Is it possible to have a service with multiple threads in it? How to achieve this?

  • you can't have more than one thread in a service.
  • you can create multiple threads in a service.
  • option 2 is possible by creating thread in onCreate().
  • option 2 is possible by creating thread in onStartCommand() of your service class.

Answer: D option 2 is possible by creating thread in onStartCommand() of your service class.
Description: Services are possible with single thread and multiple threads also. If you want multi threaded service, then write thread creation logic in onStartCommand() because this function will be called every time some one starts the service. if you want single threaded service then create thread in onCreate() of service class. Multi threaded services are also possible with AsyncTasks.

10

How to pass data from activity to service?

  • pass data in intent-putextras, and using setResult()
  • pass data in intent-putextras, and say startService() with that intent
  • store it in common database and access it through both activity and service.
  • can be done using both the ways 2 and 3 options.

Answer: D can be done using both the ways 2 and 3 options.
Description: There are many ways to do it. one straight forward way is pass data in intent-putextras, and say startService() with that intent.
one more way is store it in common database or shared preference file and access it through both activity and service.

11

Updating UI from service: How to access progress bar from a service?

  • Send progressbar id through intent extras & access it in service
  • Make progress bar as static variable in activity, and access that variable from service.
  • If you want to touch UI from service, trigger a dynamically registered receiver in activity from service. And update UI from that dynamic receiver with in that activity.
  • put all UI controls in a common class and access it from all components.

Answer: C If you want to touch UI from service, trigger a dynamically registered receiver in activity from service. And update UI from that dynamic receiver with in that activity.
Description: Both option 1 is wrong way of communication design. all UI controlling has to be done in activity to reduce side effects. if a services wants to touch UI, send a broadcast from service which triggers a receiver in activity which is dynamically registered in activity for communication. from that receiver you can touch UI since it is inner class of it.

12

What is the difference between startservice and bindservice?

  • started service - runs in background for ever unless some one or itself stops. it is used to perform long running operation. Binded service - is alive as long as some one binds to it and interacts with it. binded services can return value to the person who bound to it.
  • started service - runs in background for ever unless some one stops. started services can return values to person who started it. Binded service - is alive as long as some one binds to it and interacts with it, and after work is done, the person who has bind to it has to unbind it.
  • started service - runs in background for ever in different thread. Binded service - is alive in separate thread as long as some one binds to it and interacts with it
  • started service - run sin background and is alive as long as the component who started is also alive. Binded service - is alive in background as long as component who connected to it is also alive.

Answer: A started service - runs in background for ever unless some one or itself stops. it is used to perform long running operation. Binded service - is alive as long as some one binds to it and interacts with it. binded services can return value to the person who bound to it.
Description: Started service - is used to do some long running operation in the back ground. it will be alive in memory even if the person who started it is no longer in memory. either it itself can stop or some other also can stop it. generally services will not have UI. it is used to some back ground functionalities like sending SMS, downloading files, etc..

Binded service- is like a client server architecture where clients can request to binded service to execute some function and return the result. started services will not return results generally. since data flow happens from service to client, there should be communication channel in the case of binded services.

13

What are the various return values of onStartCommand() , and when to use what?

  • START_STICKY - in case if android stops our service forcefully, then restart service by sending intent null
  • START_NOT_STICKY - in case if android stops our service forcefully, then don't restart service, until user restarts it.
  • START_REDELIVER_INTENT - in case if android stops our service forcefully, then restart service by sending re-sending the intent.
  • all options are true

Answer: D all options are true
Description: START_STICKY - in case if android stops our service forcefully, then restart service by sending intent null. START_NOT_STICKY - in case if android stops our service forcefully, then don't restart service, until user restarts it. START_REDELIVER_INTENT - in case if android stops our service forcefully, then restart service by re-sending the intent.

14

How to monitor service connection status in bound services?

  • using isServiceConnected(), it returns true if service is connected
  • using onServiceDisConnected(), this function will be called if connection is broken
  • client can keep polling to service if it is available or not.
  • using ServiceConnection class.

Answer: B using onServiceDisConnected(), this function will be called if connection is broken
Description: client has to implement ServiceConnection object, to monitor the connection status between client and service. if service connection is broken, android will automatically call onServiceDisconnected() function of ServiceConnection object.
If service is connected then android will automatically call onServiceConnected() function of ServiceConnection object.

15

Bound service: Let’s say my service supports both starting a service and binding a service, and currently two persons have started my service and one person is binding to my service. After 5 minutes person who bound to my service, unbinds it.And other person stops my service, now is my service running in memory or got moved out from memory?

  • Service is dead but still in memory
  • Service is dead and moved out of memory
  • Service is still alive in the memory.
  • service is still alive but moved out of memory.

Answer: B Service is dead and moved out of memory
Description: Even if one client says stopService(), then service will be dead and moved out of memory if there are no binded connections are no other startService() requests pending to be processed.

16

How to implement IPC (Inter process communication) using binders?

  • 1. create a service & implement onCreate(), onBind(), onUnbind(), onDestroy()
    2. create .aidl file with interface functions.
    3. implement auto generated Binder stub class in service.
    4. return object to this stub class from onBind()
  • 1. create a service & implement onCreate(), onBind(), onStartCommand() , onUnbind(), onDestroy()
    2. create .aidl file with interface functions.
    3. implement auto generated Binder stub class in service.
    4. return object to this stub class from onStartCommand()
  • 1. create a service & implement onCreate(), onBind(), onStartCommand() , onUnbind(), onDestroy()
    2. create a inner class in Service class that extends Binder class
    3. Implement functions to be exposed in this inner class
    4. return object to this inner class from onStartCommand()
  • 1. create a service & implement onCreate(), onBind(), onUnbind(), onDestroy()
    2. create a inner class in Service class that extends Binder class
    3. Implement functions to be exposed in this inner class
    4. return object to this inner class from onBind()

Answer: A 1. create a service & implement onCreate(), onBind(), onUnbind(), onDestroy()
2. create .aidl file with interface functions.
3. implement auto generated Binder stub class in service.
4. return object to this stub class from onBind()
Description: 1. create a service & implement onCreate(), onBind(), onUnbind(), onDestroy()
2. create .aidl file with interface functions.
3. implement auto generated Binder stub class in service.
4. return object to this stub class from onBind()

17

Service or thread: To do background functionality in activity, is it better to start service or thread? why?

  • you can use both, because both will do same thing.
  • If that background functionality is co-related with activity, then use thread. else launch it in a new service with thread in the service.
  • whether that background functionality has any thing to do with activity or not, it is always better to use service with a thread, rather than using thread with activity.
  • if you want some background functionality in activity, directly for only thread. no need of service there.

Answer: B If that background functionality is co-related with activity, then use thread. else launch it in a new service with thread in the service.
Description: It always depends on the requirement. if the background functionality is related or tightly coupled with activity, then use thread. but if you want some thing to do in background irrespective of whether activity is alive or not, then go for service-with thread.

18

Is it possible to have a service without any thread?

  • yes, you can have service running in main thread
  • No, service should have a thread, it can't run in main thread
  • option 1 is O.K only if you are not running any activity in Main thread.
  • Thread and service is no way related

Answer: C option 1 is O.K only if you are not running any activity in Main thread.
Description: Service is a component that performs some operation in the background with out having UI. But it doesn't mean that it will have separate thread to do it. By default if programmer doesn't provide any thread for the service, then it runs in Main UI thread. Since it is not good practice to run Activity & Services in single thread, it is suggestible to have separate thread for the service, except in few cases like where a given application is not having any activity in it.

19

What will happen if you start a service with out any thread, that does heavy functionality?

  • i. Nothing will happen, it runs finely.
  • ii. May lead to ANR (application not responding) error some times.
  • iii. option 2 is right, but that is applicable if your application is having at least one activity, since that activity will run in UI thread.
  • iv. option 2 is right, whether you have at least one activity or not in your application.
  • i
  • ii
  • iii
  • iv

Answer: C iii
Description: Since android gives only one thread per application, default all activities will run in that thread. but service also performs long running operations in the background, it is not suggestible to run service also in the same thread which may hang your activities which lead to ANR.

20

In case of low memory if android closes a service forcefully, then will it restart automatically or user has to start that service?

  • It will never be restarted again by Android.
  • User has to restart it again.
  • Option 1 is true only if programmer has not returned START_NOT_STICKY from onStartCommand()
  • Option 1 is true if programmer has returned START_STICKY or START_NOT_STICKY

Answer: C Option 1 is true only if programmer has not returned START_NOT_STICKY from onStartCommand()
Description: If android has stopped service with out user's knowledge, then it is the responsibility of android to restart it. But this rule will not be applicable if programmer returns START_NOT_STICKY from onStartCommand().