3. Interaction
• Mobile applications on most platforms
are isolated from each other, and have
strict limitations applied to their
interaction with hardware and native
components
• Android applications use Intents,
Broadcast Receivers, Adapters, Content
Providers, and the Internet to interact
through those boundaries
4. Intents
• A message-passing mechanism (within
and between applications)
• Intents can be used to:
‣ Declare
your
inten.on
that
an
Ac.vity
or
Service
be
started
to
perform
an
ac.on,
usually
with
(or
on)
a
par.cular
piece
of
data
‣ Broadcast
that
an
event
(or
ac.on)
has
occurred
‣ Explicitly
start
a
par.cular
Service
or
Ac.vity
5. Using Intents to Launch Activities
• To open an Activity, call startActivity,
passing in an Intent
‣ Explicitly
(Explicitly
specify
the
Ac.vity
class
to
open)
‣ Implicitly
(Include
an
ac.on
that
an
Ac.vity
must
perform)
• Intent resolution: Decide what activity
is chosen to take that action (for
implicitly)
6. Explicitly Starting New Activities
(1/2)
1. Create a new Intent
2. Specifying the current application
Context and Activity class to launch
3. Pass this Intent in to startActivity
7. Explicitly Starting New Activities
(2/2)
• After startActivity is called, the new
Activity will be created and become
visible and active, moving to the top of
the Activity stack
• Calling finish on the new Activity, or
pressing the hardware back button, will
close it and remove it from the stack
8. Implicit Intents (1/3)
• A mechanism that lets anonymous
application components service action
requests (without knowing exactly which
application you’re borrowing functionality
from)
• Android will at run time resolve it into
the Activity class best suited to
performing the required action on the
type of data specified
9. Implicit Intents (2/3)
• Example: Let users make calls from
your application
‣ Implement
a
new
dialer
?
‣ Use
an
implicit
Intent
that
requests
the
ac.on
(dialing)
be
performed
on
a
phone
number
10. Implicit Intents(3/3)
• Various native applications provide
Activities to handle actions performed
on specific data
• Third-party applications, including
your own, can be registered
‣ To
support
new
ac.ons
‣ To
provide
an
alterna.ve
provider
of
na.ve
ac.ons
11. Returning Results from Activities
• startActivity will not provide any
feedback
• You can start an Activity as a sub-
Activity that’s inherently connected to
its parent
‣ A
sub-‐Ac.vity
triggers
an
event
handler
within
its
parent
Ac.vity
when
it
closes
‣ :
One
Ac.vity
is
providing
data
input
for
another
12. Sub-Activities
• Just Activities opened in a different way
• Must be registered in the application
manifest
• Any manifest-registered Activity can be
opened as a sub-Activity (system or
third-party)
13. Launching Sub-Activities (1/3)
• startActivityForResult
‣ Also
pass
in
a
request
code
(will
later
be
used
to
uniquely
iden.fy
the
sub-‐Ac.vity)
14. Launching Sub-Activities (2/3)
• Sub-Activities can be started implicitly
or explicitly
• When your sub-Activity is ready to
return, call setResult before finish to
return a result to the calling Activity
15. Launching Sub-Activities(3/3)
• The setResult method takes 2
parameters: the result code and the
result itself (represented as an Intent)
• Example: A sub-Activity’s onCreate
method, and shows how an OK and
Cancel button might return different
results to the calling Activity (next
page)
16. If the Activity is closed by the user pressing the hardware back key, or finish is called
without a prior call to setResult, the result code will be set to RESULT_CANCELED and the
result Intent set to null
17. Handling Sub-Activity Results
• When a sub-Activity closes, the
onActivityResult event handler is fired
within the calling Activity (override this
method)
• The onActivityResult handler receives a
number of parameters
‣ Request
code
‣ Result
code
‣ Data
18.
19. Native Android Actions
• Native Android applications also use
Intents to launch Activities and sub-
Activities
• When creating implicit Intents you can
use these actions (called Activity
Intents) to start Activities and sub-
Activities within your own applications
‣ ACTION_ANSWER,
ACTION_CALL,
ACTION_EDIT,
ACTION_PICK,
etc...
20. Using Intent Filters to Service
Implicit Intents
• How does Android know which
application (and component) can
service the request ?
• Intent Filters are used to register
Activities, Services, and Broadcast
Receivers as being capable of
performing an action on a particular
kind of data
21. Registering an Activity as an Intent
Receiver(1/2)
• Add an <intent-filter> tag to the
component’s manifest node using the
following tags (and associated
attributes) within the Intent Filter
node
22. Registering an Activity as an Intent
Receiver(2/2)
• action
‣ Each
Intent
Filter
must
have
one
(and
only
one)
ac.on
tag.
Ac.ons
should
be
unique
strings
that
are
self-‐
describing
• category
‣ Specify
under
which
circumstances
the
ac.on
should
be
serviced
(ALTERNATIVE,
SELECTIVE_ALTERNATIVE,
BROWSABLE,
etc...)
• data
‣ Specify
which
data
types
your
component
can
act
on
23. How Android Resolves Intent Filters
1. Listing all the Intent Filters available
from the installed packages
2. Matching the action or category
associated with the Intent
3. Matching data
4. If more than one component is resolved
from this process all the matching
possibilities are offered to the user
24. Using the Launch Intent Within an
Activity
• When an application component is started
through an implicit Intent, it needs to find the
action to be performed and the data to be
performed on
• Use the getData and getAction methods to find
the data and action associated with the Intent
25. Passing on Responsibility
• Use the startNextMatchingActivity
method to pass responsibility for
action handling to the next best
matching application component
26. Using Intent Filters for Plug-Ins and
Extensibility
• A plug-in model for your Activities
that lets them take advantage of
future functionality
• Using Intent Filters to populate
menus dynamically at run time
27. Supplying Anonymous Actions to
Applications (1/2)
• Make your Activity’s actions available
anonymously for existing applications
‣ Publish
them
using
intent-‐filter
tags
within
their
manifest
nodes
‣ The
category
tag
must
be
either
ALTERNATIVE
or
SELECTED_ALTERNATIVE
or
both
‣ The
text
used
for
the
Menu
Items
is
specified
by
the
android:label
aVribute
30. Introducing Linkify
• Linkify is a helper class
‣ Automagically
creates
hyperlinks
within
Text
View
(and
Text
View-‐derived)
classes
through
RegEx
paVern
matching
‣ Text
that
matches
a
specified
RegEx
paVern
will
be
converted
into
a
clickable
hyperlink
‣ Implicitly
fires
startAc/vity(new
Intent
(Intent.ACTION_VIEW,
uri)),
using
the
matched
text
as
the
target
URI
31. The Native Linkify Link Types
• How to linkify a Text View to display
web and e-mail addresses as
hyperlinks
• You can also linkify Views from within a
layout resource using the android:autoLink
attribute
32. Using Intents to Broadcast Events
• Intents are capable of sending structured
messages across process boundaries
• By broadcasting an event using an Intent
you let yourself and third-party developers
react to events without having to modify
your original application
• Android uses broadcast Intents to
broadcast system events like battery-
charging levels, network connections, and
incoming calls
33. Broadcasting Events with Intents
• Construct the Intent you want to broadcast
and use the sendBroadcast method to send it
• The Intent action string is used to identify
the event being broadcast, so it should be a
unique string that identifies the event
34. Broadcast Receivers(1/2)
• For a Broadcast Receiver, it needs to
be registered, either in code or within
the application manifest
35. Broadcast Receivers(2/2)
• Applications with Broadcast Receivers registered
in the manifest don’t have to be running when the
Intent is broadcast for the receivers to execute.
They will be started automatically when a matching
Intent is broadcast
• The five-second execution limit ensures that major
processing cannot, and should not, be done within
the Broadcast Receiver itself (update content,
launch Services, update Activity UI, etc...)
36.
37. Registering Broadcast Receivers in
Your Application Manifest
Even when the application has been killed or hasn’t been started
Only when the application component it is registered within is running
38. Broadcasting Sticky and Ordered
Intents
• Problem: When broadcasting an Intent
using sendBroadcast, your Intent will
be received by all registered
Broadcast Receivers, but you cannot
control the order and they cannot
propagate results
39. Native Android Broadcast Actions
• You can use these messages to add
functionality to your own projects based
on system events such as time-zone
changes, data-connection status, incoming
SMS messages, or phone calls
• ACTION_CAMERA_BUTTON,
ACTION_SCREEN_OFF,
ACTION_TIMEZONE_CHANGED,
etc...
40. Pending Intents
• The Pending Intent class provides a
mechanism for creating Intents that
can be fired by another application at
a later time
• Commonly used to package an Intent
that will be fired in response to a
future event
41. Introducing Adapters
• Adapters are bridging classes that bind
data to Views (such as List Views) used
in the user interface
• Views that support Adapter binding
must extend the AdapterView abstract
class
• You can create your own AdapterView
derived controls and to create new
Adapter classes to bind them
42. Some Native Adapters
• ArrayAdapter
‣ Array
Adapter
uses
the
toString
value
of
each
object
in
the
array
to
create
and
populate
Text
Views
• SimpleCursorAdapter
‣ You
specify
an
XML
layout
defini.on,
and
then
bind
each
column
to
a
View
within
that
layout
43. Customizing the Array Adapter
• Customize the layout used to
represent each View
• Extend ArrayAdapter with a type-
specific variation, overriding the
getView method to assign object
properties to layout Views
44.
45. Using Adapters for Data Binding
• Apply an Adapter to an AdapterView
derived class: Call the View’s
setAdapter, passing in an Adapter
instance
46. Using Internet Resources
• Benefits (client native applications rather
than entirely web-based solutions)
‣ Bandwidth:
By
crea.ng
a
na.ve
applica.on
you
can
limit
the
bandwidth
requirements
to
updated
data
only
‣ Caching:
A
na.ve
applica.on
can
cache
data
to
provide
as
much
func.onality
as
possible
without
a
live
connec.on
‣ Na4ve
features:
combine
the
data
available
online
with
the
hardware
features
available
on
the
device
47. Connecting to an Internet Resource
• Add an INTERNET uses-permission node to
your application manifest
‣ <uses-‐permission
android:name="android.permission.
INTERNET"
/>
48. Using Internet Resources
• 2 extreme
‣ Include
a
WebKit-‐based
browser
View
within
an
Ac.vity
‣ Use
client-‐side
APIs
to
interact
directly
with
server
processes
• Between
‣ Process
remote
XML
feeds
to
extract
and
process
data
using
a
Java-‐based
XML
parser
such
as
SAX
or
the
more
efficient
XmlPul
lParser
49. Introducing Dialogs
• Answer questions
• Make selections, and
confirm actions
• Display warning or error
messages
50. Implement Dialog
• Using the Dialog class (or its extension)
‣ Android
includes
a
number
of
specialist
classes
that
extend
Dialog
‣ A
Dialog-‐class-‐based
screen
is
constructed
en.rely
within
its
calling
Ac.vity,
so
it
doesn’t
need
to
be
registered
in
the
manifest
(its
life
cycle
is
controlled
en.rely
by
the
calling
Ac.vity)
• Dialog-themed Activities
• Toasts
52. The Alert Dialog Class
• Presenting a message to the user
offering them one to three options in
the form of buttons (OK, Cancel,Yes
or No)
• Offering a list of options in the form
of checkboxes or radio buttons
• Providing a text entry box for user
input
53. Alert Dialog used to display a message
and offer two button options to continue
55. Using Activities as Dialogs
• When: Need more control over the content and
life cycle of your dialog box
‣ The
solu.on
is
to
implement
it
as
a
full
Ac.vity
• The easiest way to make an Activity look like a
dialog is to apply the android:style/Theme.Dialog
theme when you add it to your manifest
• Cause your Activity to behave as a Dialog, floating
on top of, and partially obscuring, the Activity
beneath it
56. Managing and Displaying Dialogs
• Creating new instances of a dialog
each time it’s required (☹)
• Android provides the onCreateDialog
and onPrepareDialog event handlers
within the Activity class to persist and
manage dialog-box instances
• showDialog -> onCreateDialog (if
necessary) -> onPrepareDialog