What are the various return values of onStartCommand(), and when to use what?
Options
1) START_STICKY - in case if android stops our service forcefully, then restart service by sending intent null
2) START_NOT_STICKY - in case if android stops our service forcefully, then don't restart, until user restarts it.
3) START_REDELIVER_INTENT- in case if android stops our service forcefully, then restart service by sending re-sending the intent.
4) all options are true
Updating UI from service: How to access progress bar from a service?
Options
1) Send progressbar id through intent extras & access it in service
2) Make progress bar as static variable, and access that variable from service.
3) 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
4) put all UI controls in a common class and access it from all components.
How to pass data from activity to service?
Options
1) pass data in intent-putextras, and using setResult()
2) pass data in intent-putextras, and say startService() with that intent
3) store it in common database and access it through both activity and service.
4) can be done using both the ways 2 and 3 options.
Is it possible to have a service with multiple threads in it? How to achieve this?
Options
1) you can't have more than one thread in a service.
2) you can create multiple threads in a service
3) option 2 is possible by creating thread in onCreate()
4) option 2 is possible by creating thread in onStartCommand() of your service class.
How to update UI from a service that has threads?
Options
1) Create a thread in the Service class and directly access UI components of your activity
2) Since updating UI from other thread directly is not possible, communicate with Main UI thread for the UI updates
3) Use Intent Service
4) Either use option 1 or option 2
How to create a service with multiple threads in it?
Options
1) Create a service with creating thread in onStartCommand
2) Use Intent Service
3) Create a service with one thread in OnCreate().
4) Either use option1 or use Async task with service
start a service from activity and close activity, what will happen to that service, will it be alive or dead?
Options
1) service will also be killed
2) service will be alive for some time, and will be killed by android garbage collector.
3) service will run for ever, no body can stop it now, and it leaks memory.
4) 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.
What is the difference between service and a thread?
Options
1) Service - is a component of android, which runs in the background with out any UI. Service will have default one thread to run in background. Thread - is similar to service, it also runs in the background.
2) 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.
3) Option 2 is right, but one can use Thread in place of Service, no problem will come.
4) Option 1 is right, but it may have UI also some times.