You are on page 1of 7

xong s dng Explicit, gi n lt Implicit Intent.

Trc khi i vo v d, hy
do qua 1 cht kin thc v Intent Filter v vai tr ca n.

Intent Filter l g
Activity, Service v BroadCast Receiver s dng Intent Filter thng bo cho h
thng bit cc dng Implicit Intent m n c th x l. Ni cch khc, Intent Filter
l b lc Intent, ch cho nhng Intent c php i qua n.

Intent Filter m t kh nng ca component nh ngha n. Khi h thng bt c 1
Implicit Intent (ch cha 1 s thng tin chung chung v action, data v category...),
n s s dng nhng thng tin trong Intent ny, kim tra i chiu vi Intent Filter
ca cc component cc ng dng, sau quyt nh khi chy ng dng no thch
hp nht x l Intent bt c. Nu c 2 hay nhiu hn ng dng thch hp,
ngi dng s c la chn ng dng mnh mun.

VD:
M:
<activity android:name=".ExampleActivity"
android:label="@string/activity_name">
<intent-filter>
<action
android:name="android.intent.action.SENDTO" />
<category
android:name="android.intent.category.DEFAULT" />
<data android:scheme="sms" />
</intent-filter>
</activity>
Trn l 1 Activity vi b lc Intent cho php bt v x l cc Intent gi SMS. Hy
lu t kha
M:
andoid:scheme
T kha ny cho bit protocol (lut) x l d liu trong URI. Ni 1 cch n
gin th n l kiu ca d liu. 1 s kiu khc nh http, https, fpt, content...

Using Implicit Intent:
Yu cu: Xy dng chng trnh nhp s v gi. Lu chng trnh ca mnh
y ch xy dng n mc khi nhn nt Call ca di ng th s chy ng dng v
hin th giao din cho php nhp s. Phn gi dnh cho ai yu thch tm hiu thm
^_^ Phn ny khng h kh nhng y mnh ch mun minh ha Implicit Intent
nn s khng a vo.

B1: Khi to project: File -> New -> Android Project
Project name: Implicit Intent Example
Build Target: Chn Android 1.5
Application name: Implicit Intent Example
Package name: at.exam
Create Activity: Example
=> Kch nt Finish.

B2: y l bc quan trng nht v cng l bc c ngha duy nht trong c
project ny, cc bc cn li ch l bc ru ria m mnh thm vo cho ci project
n ra hn 1 cht. Bc ny s thm 1 b lc Intent Filter vo cho activity Example
ca chng ta bt s kin nhn nt Call ca di ng
-> Vo AndroidManifest.xml chnh sa nh sau:
M:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/andro
id"
package="at.exam"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".Example"
android:label="@string/app_name">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action
android:name="android.intent.action.CALL_BUTTON" />
<category
android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

</application>
<uses-sdk android:minSdkVersion="3" />

</manifest>
Thc cht ch l b sung thm dng ch mnh nh du thi ^_^

B3: Xy dng giao din trong main.xml, bc ny ko quan trng, ch l ru ria cho
activity c ci giao din:
M:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:paddingTop="10px"
android:id="@+id/number_display"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30px"
android:gravity="center"
android:lines="2"
android:background="#ffffff"
android:textColor="#000000"
/>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow
android:gravity="center"
android:paddingTop="30px"
>
<Button
android:id="@+id/button1"
android:layout_width="80px"
android:layout_height="80px"
android:gravity="center"
android:text="1"
android:textSize="25px"

/>
<Button
android:id="@+id/button2"
android:layout_width="80px"
android:layout_height="80px"
android:gravity="center"
android:text="2"
android:textSize="25px"
/>
<Button
android:id="@+id/button3"
android:layout_width="80px"
android:layout_height="80px"
android:gravity="center"
android:text="3"
android:textSize="25px"
/>
</TableRow>
<TableRow
android:gravity="center"
>
<Button
android:id="@+id/button4"
android:layout_width="80px"
android:layout_height="80px"
android:gravity="center"
android:text="4"
android:textSize="25px"

/>
<Button
android:id="@+id/button5"
android:layout_width="80px"
android:layout_height="80px"
android:gravity="center"
android:text="5"
android:textSize="25px"
/>
<Button
android:id="@+id/button6"
android:layout_width="80px"
android:layout_height="80px"
android:gravity="center"
android:text="6"
android:textSize="25px"
/>
</TableRow>
<TableRow
android:gravity="center"
>
<Button
android:id="@+id/button7"
android:layout_width="80px"
android:layout_height="80px"
android:gravity="center"
android:text="7"
android:textSize="25px"

/>
<Button
android:id="@+id/button8"
android:layout_width="80px"
android:layout_height="80px"
android:gravity="center"
android:text="8"
android:textSize="25px"
/>
<Button
android:id="@+id/button9"
android:layout_width="80px"
android:layout_height="80px"
android:gravity="center"
android:text="9"
android:textSize="25px"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<Button
android:id="@+id/button_star"
android:layout_width="80px"
android:layout_height="80px"
android:gravity="center"
android:text="*"
android:textSize="25px"
/>
<Button
android:id="@+id/button0"
android:layout_width="80px"
android:layout_height="80px"
android:gravity="center"
android:text="0"
android:textSize="25px"
/>
<Button
android:id="@+id/button_clear"
android:layout_width="80px"
android:layout_height="80px"
android:gravity="center"
android:text="Clear"
android:textSize="25px"
/>
</TableRow>
</TableLayout>
</LinearLayout>
LinearLayout cha 1 TextView hin th s nhn, 1 TableLayout c cc Button
tng ng vi cc s v 1 Button clear cho TextView.

You might also like