0% found this document useful (0 votes)
30 views20 pages

Understanding Android Intents Explained

Hướng dẫn về các loại intent và cách sự dụng trong lập trình Android, giáo án Android 1 SGU

Uploaded by

teky.player1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views20 pages

Understanding Android Intents Explained

Hướng dẫn về các loại intent và cách sự dụng trong lập trình Android, giáo án Android 1 SGU

Uploaded by

teky.player1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Intent

Explicit & Implicit


The Intent Class
•Starting Activities with Intents
• Explicit Activation
• No data result
• Data result
• Implicit Activation via Intent resolution
•A data structure that represents
• An operation to be performed (start single act), or
• An event that has occurred (BroadcastReceivers)
Intents as Desired Operations
• Intents provide a flexible language for specifying operations to
performed
• E.g., Pick a contact, take a photo, dial a phone number

• Intent is constructed by one component that wants some works done


• Received by one activity that can perform that work
The Intent class
• An Android Intent is an abstract description of an operation to be
performed.
• It can be used with startActivity to launch an Activity, broadcastIntent to
send it to any interested BroadcastReceiver components, and
startService(Intent) or bindService(Intent, ServiceConnection, int) to
communicate with a background Service.
• For example, let's assume that you have an Activity that needs to launch an
email client and sends an email using your Android device. For this
purpose, your Activity would send an ACTION_SEND along with
appropriate chooser, to the Android Intent Resolver. The specified chooser
gives the proper interface for the user to pick how to send your email data.
• Below syntax is calling startActivity method to start an
The Intent class email activity and result should be as shown below −
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.
• Action
• This is mandatory part of the Intent object and is a
string naming the action to be performed — or, in the
case of broadcast intents, the action that took place
and is being reported. The action largely determines
how the rest of the intent object is structured . The
Intent class defines a number of action constants
corresponding to different intents. Here is a list of
Android Intent Standard Actions
• The action in an Intent object can be set by the
setAction() method and read by getAction().
Intent Objects

• Data
• Adds a data specification to an intent filter.
The specification can be just a data type (the
mimeType attribute), just a URI, or both a data
type and a URI. A URI is specified by separate
attributes for each of its parts −
• The setData() method specifies data only as a
URI, setType() specifies it only as a MIME type,
and setDataAndType() specifies it as both a
URI and a MIME type. The URI is read by
getData() and the type by getType().
Intent Objects
• Category
• The category is an optional part of Intent object and it's a
string containing additional information about the kind of
component that should handle the intent. The addCategory()
method places a category in an Intent object,
removeCategory() deletes a category previously added, and
getCategories() gets the set of all categories currently in the
object. Here is a list of Android Intent Standard Categories.

• You can check detail on Intent Filters in below section to


understand how do we use categories to choose appropriate
activity corresponding to an Intent.
Intent Objects

• Extras
• This will be in key-value pairs
for additional information that
should be delivered to the
component handling the intent.
The extras can be set and read
using the putExtras() and
getExtras() methods
respectively. Here is a list of
Android Intent Standard Extra
Data
Intent Objects
• Flags
• These flags are optional part of Intent object and
instruct the Android system how to launch an
activity, and how to treat it after it's launched etc.
• Component Name
• This optional field is an android ComponentName
object representing either Activity, Service or
BroadcastReceiver class. If it is set, the Intent object
is delivered to an instance of the designated class
otherwise Android uses other information in the
Intent object to locate a suitable target.
• The component name is et by setComponent(),
setClass(), or setClassName() and read by
getComponent().
Types of Intents

• Explicit Intents
• Explicit intent going to be connected internal
world of application, suppose if you wants to
connect one activity to another activity, we
can do this quote by explicit intent, next image
is connecting first activity to second activity by
clicking button.
Types of Intents
• Implicit Intents
• These intents do not name a target and the field for
the component name is left blank. Implicit intents are
often used to activate components in other
applications
startActivityForResult vs registerActivityForResult
• startActivity() is asynchronous. The other activity will not show up until sometime later,
particularly after you return from whatever callback you were in when you called startActivity()
(e.g., onClick() of some [Link]).
• Normally, this is not much of a problem. However, sometimes one activity might start another,
where the first activity would like to know some “results” from the second. For example, the
second activity might be some sort of “chooser”, to allow the user to pick a file or contact or song
or something, and the first activity needs to know what the user chose. With startActivity() being
asynchronous, it is clear that we are not going to get that sort of result as a return value from
startActivity() itself.
• To handle this scenario, there is a separate startActivityForResult() method. While it too is
asynchronous, it allows the newly-started activity to supply a result (via a setResult() method)
that is delivered to the original activity via an onActivityResult() method. Nowadays, though,
Google prefers that we wrap these calls up using ActivityResultContracts, particularly for SDK-
supplied “contracts” for common operations, such as picking a contact.
startActivityForResult vs registerActivityForResult
• The old way
startActivityForResult vs
registerActivityForResult

The new way


Explicit with extra activity_main.xml - RelativeLayout
Explicit with extra
ActivityResul
tLauncher
Bài tập 03
• Gọi danh bạ để duyệt. Bấm
chọn xong sẽ gọi intent
tương ứng để view contact
đã chọn.
• Gọi camera chụp hình QR. Với mỗi data trong QR, gọi
Bài tập 04 intent tương ứng để view. Bao gồm: url, email, sms,
image

You might also like