Intents in android are used as message passing mechanism that works both within your application and between applications. For example intents can be used to start an activity (Ex: Gmail App) to send email. Three of the core components of an application — activities, services, and broadcast receivers are activated through messages, called intents. Intents can do the following :
- Explicitly start a service or Activity
- Start an activity to perform an action with an input
- Broadcast event
Intents explained with Example
Lets get to understand android intents with an example below. As mentioned earlier intents are message passing mechanism between application. As shown below in the image, the message from custom email app is transferred to native Gmail App using intents. you can very well see the To address, Subject and body data being passed to another application. Take a detailed look at this example here.
|Android Explicit Intent example|
Intents to Broadcast events
Intents can also be used to broadcast messages across the systems. Applications can register Broadcast receiver to listen for and react to these Broadcast Intents. The native android applications such as the Phone Dialer, SMS Manager simply register components that listen for specific Broadcast Intents - such ad "incoming phone call" or "SMS message received" and react accordingly.
Types of Android Intents
Intents are of two types :
- Explicit Intents
- Implicit Intents
Explicit Intents - Specify the class to be loaded Explicitly.
Intent intent= new Activity(MyActivity.this, MyOtherActivity.class); startActivity(intent);
Implicit Intents - In implicit intent, the sending component does not exactly specify the receiving component.
For example the below code opens the link in the browser, but all the installed browsers will be registered for this activity. So the dialog chooser will be opened requesting user action to select the browser.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.compiletimeerror.com")); startActivity(intent);
Hope this provides a brief overview of Android Intents.