You are on page 1of 14

Mobile Application Development

Lecture # 8

Intents and Intent Filters


 
Overview
• What is Intent?
• Calling Built-in Applications using intents   
• Intent Types
• Pass and Retrieving Data
• Intent Filters  
 
Intent
An  Intent is a messaging object you can use to request an action from another app component. Although intents
facilitate communication between components in several ways.

There are three fundamental use-cases

To start an activity

To start a service

To deliver a broadcast


To start an activity
•An Activity  represents a single screen in an app. You can start a new instance of
an Activity  by passing an Intent  to startActivity().

startActivity(new Intent(this, DisplayMessageActivity.class));

•The Intent describes the activity to start and carries any necessary data.

•If you want to receive a result from the activity when it finishes, call 
startActivityForResult().

startActivityForResult(new Intent(this, DisplayMessageActivity.class));

•Your activity receives the result as a separate Intent object in your activity's


onActivityResult() callback.
To start a service
•A Service  is a component that performs operations in the background without a
user interface.

•You can start a service to perform a one-time operation (such as download a file)
by passing an Intent  to startService().

•The Intent describes the service to start and carries any necessary data
To deliver a broadcast
•A broadcast is a message that any app can receive.

•The system delivers various broadcasts for system events, such as when the
system boots up or the device starts charging.

•You can deliver a broadcast to other apps by passing an Intent  to sendBroadcast()


, sendOrderedBroadcast(), or sendStickyBroadcast().
Calling Built-in Applications using intents
Intent i = new Intent(android.content.Intent.ACTION_VIEW,​​
Uri.parse(“http://www.amazon.com”));
​startActivity(i);

Intent i = new ​Intent(android.content.Intent.ACTION_DIAL,


​Uri.parse(“tel:+651234567”));
​startActivity(i);

Intent i = new Intent(android.content.Intent.ACTION_VIEW,


Uri.parse(“geo:37.827500,122.481670”));
​startActivity(i);
Intent Objects
An Intent object is a bundle of information which is used by the component that receives the
intent as well as information used by the Android system.

An Intent object can contain the following components:

Action
Data
Category
Extras
Flags
Intent Types
•There are two types of intents:

•Explicit intents specify the component to start by name (the fully-qualified class


name). You'll typically use an explicit intent to start a component in your own
app, because you know the class name of the activity or service you want to start.
For example, start a new activity in response to a user action or start a service to
download a file in the background.

•Implicit intents do not name a specific component, but instead declare a general
action to perform, which allows a component from another app to handle it. For
example, if you want to show the user a location on a map, you can use an
implicit intent to request that another capable app show a specified location on a
map.
Explicit intents
Intent CodeLearnFirstIntent = new Intent (getApplicationContext(), SecondActivity.class);
Implicit intents
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
Pass and Retrieving Data
Intent openNewActivity = new Intent(getApplicationContext(), Activity2.class);
startActivity(openNewActivity);

Pass data:
 openNewActivity.putExtra("UserName", "Pranay");
openNewActivity.putExtra("isRegistered", true);

 Bundle dataBundle = new Bundle();


dataBundle.putString("BundleUserName", "Pranay");
openNewActivity.putExtra("data",dataBundle);

Retrieving data:
Intent intentObject = getIntent();
String userName = intentObject.getStringExtra("UserName");
boolean isRegistered = intentObject.getBooleanExtra("isRegistered", true);
Intent Filters
You have seen how an Intent has been used to call an another activity.

Android OS uses filters to pinpoint the set of Activities, Services, and Broadcast receivers
that can handle the Intent with help of specified set of action, categories, data scheme
associated with an Intent.

You will use <intent-filter> element in the manifest file to list down actions, categories
and data types associated with any activity, service, or broadcast receiver.

<activity android:name=".CustomActivity" android:label="@string/app_name">


<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="com.example.My Application.LAUNCH" /> <category
android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>
</activity>
References
• http://developer.android.com/guide/components/intents-filters.html

• http://www.tutorialspoint.com/android/android_intents_filters.htm

You might also like