Android Intents

knapsackyarnMobile - sans fil

14 déc. 2013 (il y a 3 années et 6 mois)

79 vue(s)

Android Intents


Mobile Application
Development
Activities and Intents
Activities are the user
interface components
Intents are the messages
that link together
Activities
Intents are used to launch
activities
Intents


The “glue” that connects different Activities.


Intent messaging is a facility for late run-time
binding between components in the same or
different applications.


Intent object is a passive data structure
holding an abstract description of an operation
to be performed.

Intent Object Attributes


Component Name
– Name of component (class) to handle
intent, and package where class is found. If included other
intent attributes optional


Action
– Action to be performed (e.g. View, Dial, Call, Edit,
Battery_Low
, your own action)


Data
– URI of the data to be acted on and the MIME type of
that data



Category
– String containing additional information about the
kind of component that should handle the intent.


Extras
– Key value pairs for other information (a Bundle)

Categories of Intents
§

Activity Intents
: Used to call Activities outside of current
application. Only one Activity can handle the Intent.

§

Broadcast Intents:
Sent out for multiple Activities to handle.
a A description of something that has happened and is being
announced.


Activity Intent Examples
Display a web page. ACTION_VIEW
http://www.olemiss.edu

Launch the Home Screen. ACTION_MAIN CATEGORY_HOME
Display contacts with phone numbers, allowing user to select one
to return to parent activity.


ACTION_GET_CONTENT
vnd.android.cursor.item
/phone

OpenIntents
:
http://www.openintents.org/en/intentstable


Broadcast Intents
An example of a Broadcast Intent would be a message sent out
by Android about the current battery level.

Any Activity can process this Intent and react accordingly—for
example, cancel an Activity if the battery level is below a
certain point.

Intent Filters
§

Specifies the types of Intents to which an
activity can respond (used to advertise
capabilities of component)
§

Located in
AndroidManifest.xml

§

Example:

<activity
android:name
=“.Activity2” …>
<intent-filter>
<action
android:name
=“net.learn2develop.ACTIVITY2” />
<category
android:name
=“
android.intent.category.DEFAULT
” />
</intent-filter>
</activity>
Calling Activities with Intents
startActivity
(new Intent(“net.learn2develop.ACTIVITY2”));!
// call to external component!
!
startActivity
(new Intent(this, Activity2.class)); !
// in current project!
!
Intent
i
= new Intent(
android.content.Intent.ACTION_VIEW
,!

Uri.parse
(
http://www.amazon.com
));!
startActivity
(
i
);!




Mapping Intents to Components
Intent Filter Collision Resolution
Explicit intents (with component name) usually used for
application-internal messages
Implicit intents (no component name) used to launch components
in other applications
Android compares Intent to Intent-filters of (other) applications to
find best match, using action, data and category
If category is given in Intent, must be listed in other component’s
intent filter, else no match
Rules:
http://developer.android.com/guide/topics/intents/intents-
filters.html

Returning Results from an Intent
Instead of calling with
startActivity
()
, must
use
startActivityForResult
()!
!
startActivityForResult
(new Intent
(“net.learn2develop.ACTIVITY2”),
request_Code
);
Protocol for returning Intent Value
Calling activity uses
startActivityForResult
to
send Intent and
request_Code
to called activity
Called activity creates an Intent, uses
setData

on the Intent to return data. Also uses
setResult
to return a result code
(RESULT_OK or RESULT_CANCELLED)
Called activity closes itself with call to
finish()!
onActivityResult
()
is invoked in calling
activity
!

Passing Data with Intent Object
Calling activity can pass data to called activity by
creating a Bundle.


Intent
i
= new (“net.learn2develop.ACTIVITY2”);!
Bundle extras = new Bundle();!

extras.putString
(“Name”, “Your name here”);!

i.putExtras
(extras);!

startActivityForResult
(I, 1);!
Calling Built-in Apps with Intents
See Try It Now with ability to access to
§

Web Browser
§

Make Calls
§

Show Map
§

Choose Contact

Content Providers
§

Store and retrieve data and make it accessible to all
applications.
§

Only way to share data across applications; there's no
common storage area that all Android packages can access
§

Android ships with a few (audio, video, images, personal
contact information, and so on)
§

You can make your own content provider
§

Content providers expose their data as a simple table on a
database model
§

http://
developer.android.com
/guide/topics/providers/content-
providers.html