You are on page 1of 31

ANDROID

DEVELOPMENT
Working with Intents
Session 5

LEARN. DO. EARN.


Agenda Working with Intent
1. Introduction to Intent
2. Explicit Intent
3. Implicit Intent
4. StartActivityForResult
5. Built-in Standard Intent Actions
6. Introduction To Data Passing
7. Intent Data Passing: Using An Explicit Bundle
8. Intent Data Passing: Directly Using putExtra
9. Pending Intent
10. Allow Others App to Start Your Activity

Working with Intents 1


Introduction to Intent
Intent is a simple message object which is used to communicate from one activity
to another and an abstract description of an operation needs to be performed
Intents define intention of an Application. They are also used to transfer data
between activities
An Android Intent can be used to perform the following 3 tasks:
Open another Activity or Service from the current Activity
Pass data between Activities and Services
Delegate responsibility to another application
For example, you can use Intents to open the browser application to display a URL.
Intent can be broadly classified into 2 categories:
Explicit Intent
Implicit Intent

Working with Intents 2


Explicit Intent
Explicit intent is the Intent in which you explicitly
define the component that needs to be called by the
Android System

An explicit intent is one that you use to launch a


specific app component, such as a particular activity
or service in your app

Syntax: Intent openNewActivity = new


Intent(getApplicationContext(),
SecondActivity.class);

Working with Intents 3


Implicit Intent
Implicit intent is the intent where instead of defining the
exact components, you define the action you want to
perform

An implicit intent specifies an action that can invoke any


app on the device and it is able to perform the action

Using an implicit intent is useful when your app cannot


perform the action, but other apps probably can and you'd
like the user to pick which app to use
Syntax:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);

Working with Intents 4


Verify There is an App to Receive the Intent

Although, the Android platform guarantees that certain intents will resolve to one of the
built-in apps (such as the Phone, Email, or Calendar app), you should always include a
verification step before invoking an intent.

To verify there is an activity available that can respond to the intent,


call queryIntentActivities() to get a list of activities capable of handling your Intent. If the
returned List is not empty, you can safely use the intent.

Working with Intents 5


Verify There is an App to Receive the Intent (Contd.)

Example:

If isIntentSafe is true, then at least one app will respond to the intent.
If isIntentSafe is false, then there aren't any apps to handle the intent.

*Note: You should check this before firing an Intent because if there would not be any
app installed in the users phone, then you might need to disable the functionality or if
you know the application which can perform that task, you can refer the user to
download that app for completing this action

Working with Intents 6


Getting a Result from an Activity
You can also start another activity and receive a result back. To receive a result,
call startActivityForResult() (instead of startActivity())

For example, your app can start a camera app and receive the captured photo as
a result. Or, you might start the People app in order for the user to select a
contact and you'll receive the contact details as a result.

Working with Intents 7


Quiz Question

Which component is not activated by an intent?

1. Activity
2. Services
3. ContentProvider
4. BroadcastReceiver

Working with Intents 8


Getting a Result from an Activity
For example, here's how to start an activity that allows the user to pick a contact:

Working with Intents 8


Receive the Result
When the user is done with the subsequent activity and returns, the system calls
your activity's onActivityResult() method. This method includes three arguments:
The request code you passed to startActivityForResult()

A result code specified by the second activity. This is either:

RESULT_OK if the operation was successful, or

RESULT_CANCELED if the user backed out or the operation failed for some
reason
An Intent that carries the result data

Working with Intents 9


Receive the Result (Contd.)
For example, here's how you can handle the result for the "pick a contact" intent:

Working with Intents 10


Built-in Standard Intent Actions
List of standard actions that Intents can use for launching activities (usually through
startActivity(Intent).

ACTION_MAIN

ACTION_VIEWACTION_EDIT

ACTION_PICK

ACTION_CHOOSER

ACTION_DIAL

Working with Intents 11


Built-in Standard Intent Actions (contd.)

ACTION_CALL

ACTION_SEND

ACTION_SENDTO

ACTION_ANSWER

ACTION_SEARCH

ACTION_WEB_SEARCH

Working with Intents 12


Introduction to Data Passing
Data Passing: There are multiple ways to store & pass data through an Intent
You can use an explicit bundle and set bundle using putExtras, or

You can directly use putExtra

Working with Intents 13


Intent Data Passing - Using an Explicit Bundle

Bundle object is used to pass data between activities. You can use the putExtras
method to associate a bundle with an Intent.
Lets see the example code:

The code mentioned below describes as to how we collect data into the
Bundle object

Working with Intents 14


Quiz Question

In an Explicit intent, the sender specifies the type of receiver.

1. True
2. False

Working with Intents 15


Intent Data Passing - Directly Using putExtra

putExtra is the method which is used to store data in an Intent object

This data can be of different types: String, Char, Boolean, Bundle, etc

This data gets set as the key value pair, which you can retrieve in the called activity
Example:

The code below describes as to how we collect data into the Intent object

Working with Intents 15


Interview Questions

What if I am trying to receive a parameter from the bundle, which I didnt send in the
previous activity ?

I want to show a particular contact number on dialer pad, what should I do ?

Can I just call the activity from the explicit intent?

Working with Intents 16


Pending Intent
A PendingIntent is a token that you give to a foreign application
NotificationManager
AlarmManager
Home Screen AppWidgetManager, or other 3rd party applications that allows
the foreign application to use your application's permissions to execute a
predefined piece of code

Working with Intents 17


Pending Intent (Contd.)

If you give the foreign application an Intent, and that application


sends/broadcasts the Intent you gave, they will execute the Intent with their own
permissions

If you instead give the foreign application a PendingIntent you created using
your own permission, that application will execute the contained Intent using your
application's permission

Working with Intents 18


Allowing Other Apps to Start Your Activity

If your app can perform an action that might be useful to another app, your app
should be prepared to respond to action requests from other apps
To allow other apps to start your activity, you need to add an <intent-
filter> element in your manifest file for the corresponding <activity> element
If your app will be installed on the users phone, then the system will identify
which activity can respond to the corresponding action and your app is compatible
then it will be listed in AppChooser for that implicit intent

Working with Intents 19


Allowing Other Apps to Start Your Activity (contd. 1)

Add an Intent Filter

In order to properly define which intents your activity can handle, each intent filter you
add should be as specific as possible in terms of the type of action and data the activity
accepts

The system may send a given Intent to an activity if that activity has an intent filter
which fulfills the following criteria of the Intent object:

Working with Intents 20


Allowing Other Apps to Start Your Activity (contd. 2)

Example:

Note: Each incoming intent specifies only one action and one data type, but it's OK
to declare multiple instances of the <action>, <category>, and <data> elements
in each <intent-filter>

Working with Intents 21


Handle the Intent in Your Activity
As your activity starts, call getIntent() to retrieve the Intent that started the
activity
You can do so at any time during the lifecycle of the activity, but you should
generally do so during early callbacks such as onCreate() or onStart()

Working with Intents 22


Return a Result

If you want to return a result to the activity that invoked yours, simply call setResult() to
specify the result code and result Intent

When your operation is done and the user should return to the original activity,
call finish() to close (and destroy) your activity.
Example:

Working with Intents 23


Return a Result (contd.)
You must always specify a result code with the result. Generally, it's
either RESULT_OK or RESULT_CANCELED

You can then provide additional data with an Intent, as necessary

Working with Intents 24


Interview Questions

Can my application be called by any other applications? If yes, What kind of intent will it
be?

When I open a notification, what kind of intent is it?

Working with Intents 25


LETS DISCUSS
THE ASSIGNMENTS

LEARN. DO. EARN


THANK YOU
Email us at: support@acadgild.com

LEARN. DO. EARN

You might also like