Wednesday, 26 October 2016

Android interview questions and answers

1) Difference between main thread(UI thread) and worker thread?
People use the word "worker" when they mean a thread that does not own or interact with UI.  Threads that do handle UI are called "UI" threads. Usually, your main (primary) thread will be the thread that owns and manages UI. And then you start one or more worker threads that do specific tasks.

2) Difference between Service and Intent Service?
Service
IntentService
1)      The Service runs in background but it runs on the Main Thread of the application.
1)      The IntentService runs on a separate worker thread.
2)      The Service can be used in tasks with no UI, but shouldn't be too long. If you need to perform long tasks, you must use threads within Service.
2)  The IntentService can be used in long tasks usually with no communication to Main Thread. If communication is required, can use Main Thread handler or broadcast intents. Another case of use is when callbacks are needed (Intent triggered tasks)
3)      The Service is triggered by calling method startService().
3)  The IntentService is triggered using an Intent, it spawns a new worker thread and the method onHandleIntent() is called on this thread.
4)      The Service may be triggered from any thread, activity or other application component.

4) IntentService may be triggered from any thread, activity or other application component.

5)      The Service may block the Main Thread of the application
5) The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.
6)       It is our responsibility to stop the service when its work is done, by calling stopSelf() or stopService().
6) The IntentService stops the service after all start requests have been handled, so you never have to call stopSelf().

3) Difference between Intent , sticky intent and pending intent?

Intent - is a message passing mechanism b/w components of android except for contentProviders
Sticky Intent - Sticks with android, for future broadcast listeners. sticky intent is a broadcast from sendStickyBroadCast() method, such that it floats arround even afyer broadcast, allowing others to collect data from it.
Pending intent - will be used when some one wants to fire an intent in future and may be at that ttime app is not alive.