Activities and Intents
Programming with Android 187
More on Activities: Activity states
In most cases, an Android Application is composed
of multiple Activities, not just one …
LOGIN Welcome Marco!
marco
Activity 1
Activity 2
PASSWORD
**********
Go to Next Activity
Login
Programming with Android 188
More on Activities: Saving resources
Each Activity has its own:
Java implementation MyActivity.java
XML Layout file activity_main.xml
Lifecycle with different states ACTIVE PAUSED STOPPED
XML Tag in AndroidManifest.xml
<application>
<activity android:name=".MyActivity" />
</application>
Programming with Android 189
Activities and AndroidManifest.xml
Each activity has its Java class and layout file.
public class FirstActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
}
public class SecondActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
}
Programming with Android 190
Intent Definition
Intent: facility for late run-time binding between
components of the same or different applications.
Call a component from another component
Possible to pass data between components
Components: Activities, Services, Broadcast receivers …
Something like:
“Android, please do that with these data”
Reuse already installed applications and components
Programming with Android 191
Intent Definition
We can think to an Intent object as a message
containing a bundle of information.
Component Name
Action Name
Data
Structure
of an Intent Category
Extra Flags
Intent is sent from current Activity to a receiver Activity which
is then activated and executed.
Programming with Android 192
Intent types
INTENT TYPES
IMPLICIT
EXPLICIT
The target receiver is specified The target receiver is specified
through the Component Name through the actions to be executed.
Used to launch specific Activities The system chooses the receiver
that matches the request.
Programming with Android 193
Intent types: Explicit Intents
Explicit Intent Specify the name of the Activity that will
handle the intent.
Component Name USED
Action Name NOT USED
Data NOT USED
Structure
of an Intent Category NOT USED
Extra Flags
Used to control the execution flow between Activities belonging to the same Android
application.
Programming with Android 194
Intent types: Explicit Intents
1. Build a new Intent message
2. Specify the Activity who will receive the Intent
3. Fire the Intent through the startActivity()
NAME OF THE ACTIVITY TO START
Intent intent=new Intent(this, SecondActivity.class);
startActivity(intent);
Programming with Android 195
Intent types: Explicit Intents
1. Build a new Intent message
2. Specify the Activity who will receive the Intent
3. Fire the Intent through the startActivity()
Intent intent=new Intent(); ALTERNATIVE code
ComponentName component=new
ComponentName(this,SecondActivity.class);
intent.setComponent(component);
startActivity(intent);
Programming with Android 196
Intent types: Explicit Intents
(OPTIONAL) Insert parameters to be sent to the called
Activity in the Extra field of the Intent.
intent.putExtra(“KEY”, VALUE);
Set an argument named “MyValue” and equal to 5.
Intent intent=new Intent(this, SecondActivity.class);
intent.putExtra(“myValue”,5);
startActivity(intent);
Programming with Android 197
Intent types: Explicit Intents
(OPTIONAL) From the called Activity, retrieve the
parameters inserted from the calling Activity
intent.getExtras().getTYPE(“KEY”);
Get an argument of type int with key equal to “myValue”
intent.getExtras().getInt(“myValue”);
intent.getExtras().getString(“myString”);
intent.getExtras().getBoolean(“myBoolean”);
Programming with Android 198
Intent types: Explicit Intents
Some Activities can return results to the caller Activity
SENDER SIDE
1. IMPLEMENT method onActivityResult(int requestCode, int
resultCode, Intent data)
2. INVOKE method startActivityForResult(Intent intent, int
requestCode)
Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, CHOOSE_ACTIVITY_CODE);
…
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Invoked when SecondActivity completes its operations
Programming with Android 199
Intent types: Explicit Intents
Some Activities can return results to the caller Activity
RECEIVER SIDE
1. IMPLEMENT method setResult(int resultCode, Intent data)
2. SET results on the Extra field of the Intent
Intent intent=new Intent();
setResult(RESULT_OK, intent);
intent.putExtra("result", resultValue);
finish();
The result is delivered only after invoking the finish() method!
Programming with Android 200
Intent types
INTENT TYPES
EXPLICIT IMPLICIT
The target receiver is specified The target receiver is specified
through the component Name through the actions to be executed.
Used to launch specific Activities The system chooses the receiver
that matches the request.
Programming with Android 201
Intent types: Implicit Intents
Implicit Intents do not name a target (component
name is left blank) but an intention of what to do …
When an Intent is launched, Android
checks out which Activies might answer
to the Intent …
If at least one is found, then that
Activity is started
Binding does not occur at compile time,
nor at install time, but at run-time
…(late run-time binding)
Programming with Android 202
Intent types: Implicit Intents
Implicit Intents do not name a target (component name is
left blank) but an intention of what to do …
Component Name NOT USED
Action Name USED
Data USED
Structure
of an Intent Category USED
Extra Flags
Used to control the execution flow between Activities belonging to DIFFERENT
Android applications.
Programming with Android 203
Intent types: Implicit Intents
Implicit Intents do not name a target (component name is
left blank) but an intention of what to do …
Component Name NOT USED
A string naming the action to be
performed.
Action Name USED
Pre-defined, or can be
specified by the programmer. Data USED
Category USED
void setAction(String)
Extra Flags
Programming with Android 204
Intent types: Implicit Intents
Special actions (http://developer.android.com/reference/android/content/Intent.html)
Action Name Description
ACTION_IMAGE_CAPTURE Open the camera and receive a photo
ACTION_VIDEO_CAPTURE Open the camera and receive a video
ACTION_DIAL Open the phone app and dial a phone number
ACTION_SENDTO Send an email (email data contained in the extra)
ACTION_SETTINGS Open the system setting
ACTION_WIRELESS_SETTINGS Open the system setting of the wireless interfaces
ACTION_DISPLAY_SETTINGS Open the system setting of the display
Programming with Android 205
Intent types: Implicit Intents
Generic actions (http://developer.android.com/reference/android/content/Intent.html)
Action Name Description
ACTION_EDIT Display data to edit
ACTION_MAIN Start as a main entry point, does not expect to receive data.
ACTION_PICK Pick an item from the data, returning what was selected.
ACTION_VIEW Display the data to the user
ACTION_SEARCH Perform a search
Action Defined by the programmer
ma.example.projectpackage.FILL_DATA (package prefix + name action)
Programming with Android 206
Intent types: Implicit Intents
1. Build a new Intent message
2. Specify only the Action you want to perform
3. Fire the Intent through the startActivity()
ACTION NAME
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent);
Programming with Android 207
Intent types: Implicit Intents
1. Build a new Intent message
2. Specify only the Action you want to perform
3. Fire the Intent through the startActivity()
4. Verify whether the Intent can be handled
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) !=
null) {
startActivity(intent);
}
Programming with Android 208
Intent types: Implicit Intents
Implicit Intents do not name a target (component name is
left blank) but an intention of what to do …
Component Name NOT USED
Data passed from the caller to the
called Component. Action Name USED
Def. of the data (URI) and Type of the Data
data (MIME type) USED
Category USED
1. void setData(Uri)
2. void setType(String) Extra Flags
Programming with Android 209
Intent types: Implicit Intents
In an Intent, the Data is specified by a name and a type
NAME: Uniform Resource Identifier (URI)
scheme://host:port/path
tel:003-232-134-126 EXAMPLEs
content://contacts/people/1
http://www.ensa.uh1.ac.ma
Programming with Android 210
Intent types: Implicit Intents
Some actions require data in input to be executed.
Use method setData(URI) to define the data input of
an Implicit Intent
Intent intent=new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0123456789"));
startActivity(intent);
Programming with Android 211
Intent types: Implicit Intents
Some actions require data in input to be executed.
Use method setData(URI) to define the data input of
an Implicit Intent
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("content://contacts/people/1"));
startActivity(intent);
Programming with Android 212
Intent types: Implicit Intents
Some actions require data in input to be executed.
Use method setData(URI) to define the data input of
an Implicit Intent
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(”http://www.ensa.uh1.ac.ma"));
startActivity(intent);
Programming with Android 213
Intent types: Implicit Intents
In an Intent, the Data is specified by a name and a type
TYPE: Multipurpose Internet Mail Extensions (MIME)
type/subtype
EXAMPLEs
image/gif image/jpeg image/png
text/html text/plain text/css
video/mpeg4 … … … …
Programming with Android 214
Intent types: Implicit Intents
Some actions require data in input to be executed.
Use method setType(MIME) to define the data input
of an Implicit Intent
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
sendIntent.setType(HTTP.PLAIN_TEXT_TYPE);
startActivity(sendIntent)
Programming with Android 215
Intent types: Implicit Intents
Implicit Intents do not name a target (component name is
left blank) but an intention of what to do …
Component Name NOT USED
A string containing information about
the kind of component that should
handle the Intent. Action Name USED
More than one can be specified for an Data USED
Intent
Category USED
void addCategory(String)
Extra Flags
Programming with Android 216
Intent Components
Category String describing the kind of component (Activity)
that should handle the Intent.
Category Name Description
CATEGORY_HOME The activity displays the HOME screen.
CATEGORY_LAUNCHER The activity is listed in the top-level application
launcher, and can be displayed.
CATEGORY_PREFERENCE The activity is a preference panel.
CATEGORY_BROWSABLE The activity can be invoked by the browser to
display data referenced by a link.
Programming with Android 217
Intent Components
1. Build a new Intent message
2. Specify only the Category of the receiver Activity
3. Fire the Intent through the startActivity()
Intent intent=new Intent();
Intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
Programming with Android 218
Intent types: Intent Resolution
QUESTION: How can Android know which application to
call after an Implicit Intent is fired?
ANSWER: Each application declares the Intent is able to
handle in the AndroidManifest.xml file
If an Intent with Action name ACTION_ECHO is invoked, the Activity is lanched
<intent-filter>
<action android:name=”ACTION_ECHO” />
</intent-filter>
Programming with Android 219
Intent types: Intent Resolution
The Intent resolution process resolves the Intent-Filter
that can handle a given Intent (e.g. ACTION_ECHO).
Three tests must be passed:
1. Action field test
2. Category field test
3. Data field test
If the Intent-filter of Activity A passes all the three test, then
it is selected to handle the Intent.
Programming with Android 220
Intent types: Intent Resolution
(ACTION Test): The action specified in the Intent must match
one of the actions listed in the filter.
If the filter does not specify any action FAIL
An Intent that does not specify an action SUCCESS as
as long as the filter contains at least one action.
<intent-filer … >
<action android:name=“com.example.ma.ECHO”/>
</intent-filter>
Programming with Android 221
Intent types: Intent Resolution
(CATEGORY Test): Every category in the Intent must match
a category of the Intent Filter.
If the category is not specified in the Intent Android
assumes it is CATEGORY_DEFAULT, thus the filter must
include this category to handle the Intent.
<intent-filer … >
<category android:name=“android.intent.category.DEFAULT”/>
</intent-filter>
Programming with Android 222
Intent types: Intent Resolution
(DATA Test): The URI of the intent is compared with
the parts of the URI mentioned in the filter.
<intent-filer … >
<data android:mimeType=“audio/* android:scheme=“http”/>
<data android:mimeType=“video/mpeg android:scheme=“http”/>
</intent-filter>
Both URI and MIME-types are compared
Programming with Android 223