What is Intent service?

Asked 07-Apr-2018
Viewed 829 times

1 Answer


0

Intent
Generally intent is a message that is pass between different components such as activities, content providers, broadcast receiver etc. It is mainly used for invoked or called the component such as-
  •  Dial a phone call
  • Display a webpage
  •  Display our all contacts
  •  Launch an activity in our project
  •  Start any type of service
  •  Broadcast a message etc.
How intent Work:

What is Intent service?

Sending Data by using Intent Class
Intent intent=new Intent (Context, Destination class);
Intent.putExtra(key,value);
startActivity(intent);

Fetching Data After intent service is Completed-
Intent intent=getIntent();
DataType variable=intent.getStringExtra(key);

Intent Services:
IntentService is base class of Service class that handle asynchronous request or task on users demands. In android if client send any requests then startService(Intent) method calls. After processing all request it automatically stop the work. IntentService can handle multiple task simultaneously.
In android when callback is needed then be use Intent. And if we cann’t communicate to Main Thread and perform log task then we use Intent Services after that if we want to communicate then we use Main Thread Handler.

The IntentService class provide the following services-
  •  For executing all the intents it create a default worker thread that are delivered to startActivityCommand() method, and also attach your application’s main thread.
  •  After that we can create work queue & pass one intent at a time on your onHandleIntent() method for implementation & it also remove the concept of multi-threading.
  •  If all the start request are handled then stop the service, and you can never use stopSelf() method for stopping the services.
  •  For provides default implementation you can use onBind() method that returns null value.
  •  At last Provide the implementation of onStartCommand() method to sends the entent on work queue and handle the onHandleIntent() method.
Finally completing the whole work provided by the user, you can implement onHandleIntent() method. However, you can provide the constructor for the IntentService.

What is Intent service?

Here's an example implementation of IntentService.

public class IntentServiceExample extends IntentService {
public  IntentServiceExample() {
  super(“IntentServiceExample”);
}
@override
protected void onHandleIntent(Intent intent) {
try  {
Thread.sleep(5000);
} catch(interruptedException e) {
Thread.currentThread().intrrupt();
}
}
}
If you want to override the different callback methods such as onCreate(), onDestroy(), onStartCommand() then you must implement the IntentService Class that handle the functionality of worker thread.

For Example – onStartCommand() method return the implementation ,which describe how the intent is deliverd to onHandleIntent() method.
@override 
Public int onStartCommand(Intent intent, int flag, int id) {
Toast.makeText(this,”Service Started”,Toast.LENGTH_LONG).show();
return.super.onStartCommand(intent, flag, id); } 
If your service allow binding then you can call onBind() method.


Comment
Thanks for giving the answer, Mis. Arti. - Anonymous User09-Apr-2018