You are on page 1of 5

Trong bi ny mnh s i su ni r v Intent, phn c bn v ng vai tr rt

quan trng trong lp trnh ng dng Android.


Khi nim v Intent:

Theo nh ngha ca Google, Intent l mt miu t v mt hot ng cn c


thc hin. Cn ni mt cch n gin v d hiu hn, Intent l mt c cu cho
php truyn thng ip gia cc thnh phn ca 1 ng dng v gia cc ng dng
vi nhau.
Cc thuc tnh ca Intent:
- action: l hnh ng c thc hin, vd : ACTION_VIEW, ACTION_MAIN
- data: l d liu s c x l trong action, thng c din t l mt Uri
(Uniform Resource Identifier, tham kho
http://en.wikipedia.org/wiki/Uniform...rce_Identifier hiu r thm chi tit).
VD:
ACTION_VIEW content://contacts/people/1 - Hin th thng tin v ngi vi m
danh 1
ACTION_DIAL content://contacts/people/1 - Hin th mn hnh gi n ngi vi
m danh 1
ACTION_DIAL tel:123 - Hin th mn hnh gi vi s gi l 123
Ngoi ra cn c 1 s thuc tnh m ta c th b sung vo Intent:
- category: b sung thm thng tin cho action ca Intent. VD:
CATEGORY_LAUNCHER thng bo s thm vo Launcher nh l mt ng dng

top-level
- type: ch r kiu ca data
- component: ch r thnh phn s nhn v x l intent. Khi thuc tnh ny c
xc nh th cc thuc tnh khc s tr thnh thuc tnh ph.
- extras: mang theo i tng Bundle cha cc gi tr b sung.
VD:
ACTION_MAIN v CATEGORY_HOME: tr v mn hnh Home ca Android (khi
bm nt Home ca di ng)
Phn loi Intent:
Intent c chia lm 2 loi:
- Explicit Intents: intent c xc nh thuc tnh component, ngha l ch
r thnh phn s nhn v x l intent. Thng thng intent dng ny s khng b
sung thm cc thuc tnh khc nh action, data. Explicit Intent thng c s
dng khi chy cc activity trong cng 1 ng dng.
- Implicit Intents: Intent khng ch r component x l, thay vo n b sung
thng tin trong cc thuc tnh. Khi intent c gi i, h thng s da vo nhng
thng tin ny quyt nh component no thch hp nht x l n.
VD:
ACTION_DIAL tel:123 thng thng s c h thng giao cho activity Phone
Dialer mc nh ca Android x l.
Mt s action thng s dng trong Intent:
ACTION_ANSWER - m Activity x l cuc gi ti, thng l Phone Dialer ca
Android
ACTION_CALL - m 1 Phone Dialer (mc nh l PD ca Android) v ngay lp tc
thc hin cuc gi da vo thng tin trong data URI
ACTION_DELETE - m Activity cho php xa d liu m a ch ca n cha trong
data URI
ACTION_DIAL - m 1 Phone Dialer (mc nh l PD ca Android) v in thng tin
ly t a ch cha trong data URI

ACTION_EDIT - m 1 Activity cho php chnh sa d liu m a ch ly t data


URI
ACTION_SEND - m 1 Activity cho php gi d liu ly t data URI, kiu ca d
liu xc nh trong thuc tnh type
ACTION_SENDTO - m 1 Activity cho php gi thng ip ti a ch ly t data
URI
ACTION_VIEW - action thng dng nht, khi chy activity thch hp hin th
d liu trong data URI
ACTION_MAIN - s dng khi chy 1 Activity

OK, l thuyt nh th l tm n. Gi chng ta s chuyn qua phn thc hnh


hiu r cch s dng Intent. Nh nu trn, Intent chia lm 2 loi: explicit
intent v implicit intent. Mi loi Intent s c cch ci t v s dng khc nhau.
Using Explicit Intents
Yu cu: Xy dng chng trnh gm 2 Activity. Activity1 l Activity chy ban
u lc khi ng ng dng, cho php nhp vo 1 gi tr, cho php khi chy
Activity2 v gi gi tr ny ti Activity2. Activity2 s nhn v hin th gi tr, ri
li gi gi tr ny ti 1 BroadcastReceiver. C ch gi v khi chy Activity s
dng thng qua Intent.
B1: Khi to project: File -> New -> Android Project
Project name: Explicit Intent Example
Build Target: Chn Android 1.5
Application name: Explicit Intent Example
Package name: at.exam
Create Activity: Activity1
=> Kch nt Finish.
B2: To giao din cho Activity1 -> res\layout\main.xml chuyn tn thnh
activity1_layout.xml
M:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Activity 1 - Send value"
android:typeface="normal"
android:textSize="14px"
android:textStyle="bold"
android:textColor="#cccccc"
android:background="#333333"
/>
<EditText
android:id="@+id/value_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20px"
android:gravity="center"
android:lines="1"
android:numeric="integer"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/send_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send to Activity 2"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</LinearLayout>

Layout cho Activity1 bao gm 1 LinearLayout cha 1 TextView, 1 EditText


nhp gi tr ( gii hn kiu nhp l number), v 1 RelativeLayout c 1 Button
khi chy Activity2. Mnh s dng RelaytiveLayout c th xp Button ny
xung pha cui ca giao din.

You might also like