InterviewSolution
| 1. |
Difference between Background Service & Intent Service? |
|
Answer» In Android Service is something that is running in the background. In a simple way, we can say that a background service is same as an Activity. The only difference is it doesn’t have UI. A background Service can be started by calling the startService method in your Activity. we need to create an explicit Intent, which means that you either make a reference to the Service’s class, or you include the package name of your APP in the Intent. For example: Intent intent = new Intent(mContext, MyService.class); startService(intent);There is one drawback with Service is that it is running on Main Thread so you can not ASSIGN any long running operation to service. But if you want to run some long running operation in the background, you should use IntentService. IntentService is CREATING its own Worker Thread which is running separately from the Main Thread. EXAMPLES for its usage would be to download certain resources from the Internet. For creating Intent Service you need to create a service class by extending IntentService and implementing onHandleIntent() method. public class TestIntentService extends IntentService{ @Override protected void onHandleIntent(Intent intent) { }In Intent Service we can start service by calling startService method passing intent which identifies the service. We can call startService any number of times, but in the background it will create only one instance of intent service and all requests are queued and processed in separate work thread. We don’t need to worry about calling stopService, as IntentService stops itself when there are no requests to serve. |
|