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.
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.
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).
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.
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.
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 ..
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.
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.
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.
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.
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.
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.
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.
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.
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.
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()
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.
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.
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.
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().