Android Intents and Types of Intents
Android Intent:
An Intent in Android is a messaging object we can use to request data or an action from another app compotent. Using Intent we can pass data between activities in an application.
There are three fundamental use cases of Intents :
1. Starting an Activity :
We can start a new instance of an Android Activity by declaring its Intent
and call startActivity()
. The Intent variable describes the new instance of activity to start and carries any necessary data.
Example :
Intent i = new Intent(ClassName.this, NewActivityName.class);
startActivity(i);
2. Starting a Service :
Android Service is a component that performs a given task without user interface, it runs in background. Using Intents we can start a Service by passing its Intent variable
to startService()
.
Example : Downloading a Song from a given URL. The Intent
describes the service to start and carries necessary data in it.
Before Android 5.0 (API Level 21), a Service can be started by using methods of service
class. With Android 5.0 and higher, services can be started using Job scheduler.
3. Delivering a broadcast message :
A Broadcast is a message that any application can receive. Android delivers multiple broadcast messages for System events, such as when the System boots up or the device starts Charging. Broadcast can be delivered to other applications by passing an Intent
to sendBroadcast()
or sendOrderedBroadcast()
.
Android Types of Intents :
There are two types of Intents in Android – Explicit Intents and Implicit Intents.
Explicit Intents :
Explicit Intent specifies which application will satisfy the intent, by supplying either the target application package or a fully-qualified component class name. In an application we normally use Explicit intent to start a new Activity instance by specifying the class name and the data to pass with it.
Example :
Intent i = new Intent(ClassName.this, NewActivityName.class);
startActivity(i);
Implicit Intent :
Implicit Intent declares a general action to perform (like downloading a file, sending a mail, opening an URL), which allows a component from another application to handle it.
Example :
Uri number = Uri.parse(“tel:0000000”);
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);