You are on page 1of 6

III-S dng Intent nh th no?

-Cc hm thc thi Activity

-Intent tng minh thc thi Activity Nh trnh by phn II, intent c th dng thuc tnh ph component ch nh ch danh tn lp s thc thi Activity. thc hin iu ny, lp Intent cung cp cc hm l setComponent(ComponentName) v setClass(Context, Class) v setClassName(Context, String) setClassName(String, String). Ch c dng gi cc Activities trong cng mt app VD: PHP Code: Intent intent = new Intent(); intent.setClassName("ten_package", "ten_lop_ben_tr ong_package"); startActivity(intent);

-Intent khng tng minh thc thi Activity Trong trng hp ny intent khng ch nh mt lp c th m thay vo dng cc d liu khc (action, data, type, etc.) v h thng t quyt nh xem lp no (app no) s thch hp p ng intent . Thng tin action v category ca activity trong mt app p ng intent phi c khai bo trong Manifest ca app (AndroidManifest.xml) di dng Intentfilter (tt nhin nu chng ta mun gi mt built-in action th ta khng cn quan tm n vn ny). VD: PHP Code: <activity class=".NotesList" android:label="@strin g/title_notes_list"> <intent-filter> <action android:name="android.intent.action.GE T_CONTENT" /> <category android:name="android.intent.categor y.DEFAULT" /> <data android:mimeType="vnd.android.cursor.ite m/vnd.google.note" /> </intent-filter> </activity>

IV-Truyn nhn thng tin gia cc Activity s dng intent


-Gi s ta xy dng mt app c hai activites A v B nh hnh v trn. Khi bn phi Activity A ta s gi hm: PHP Code: startActivity(intentA,request_code)

-Bn pha Activity B ta s gi hm: PHP Code: setResult(return_code, intentB);

Trong phn 1, mnh trnh by nhng kin thc c bn v Intent. Tip theo mnh s hng dn cc bn lm mt Tutorial n gin hiu r hn nhng vn nu trong l thuyt. -Gi s bn cn vit mt app tnh cc phn t ca mt dy s c cho theo quy lut: PHP Code: a0,b0 nhp t bn phm a(n+1)=a(n)+b(n) b(n+1)=a(n)*b(n) thc hin, chng ta xy dng hai Activities 1 v 2. Activity 1 s lm nhim v ly d liu nhp vo sau gi Activity 2 tnh ton kt qu v ly d liu tr v. Ngi dng s quyt nh tip tc tnh ton hay reset li t u. Ton b qu trnh ny c minh ha nh hnh v di y:

-u tin bn to mt New Project nh sau: PHP Code: Project Name: IntentBasic Target: bt k (vd 1.5) Package name: com.vietanddev.intent Application name: Intent Basic -Tip theo bn to layout cho hai Activities nh hnh v di

input.xml PHP Code: <RelativeLayout xmlns:android="http://schemas.android.c om/apk/res/android" android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:text="A = " android:layout_margin="20dip" android:layout_height="wrap_content"></TextView> <EditText android:id="@+id/txtNum1" android:text="0" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignBottom="@id/TextView01" android:background="@android:drawable/editbox_backg round" android:layout_marginRight="10dip" android:layout_toRightOf="@id/TextView01"></EditTex t> <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_below="@id/txtNum1" android:text="B = " android:layout_margin="20dip" android:layout_height="wrap_content"></TextView> <EditText android:id="@+id/txtNum2" android:text="0" android:layout_width="fill_parent" android:layout_height="wrap_content"

android:background="@android:drawable/editbox_backg round" android:layout_marginRight="10dip" android:layout_toRightOf="@id/TextView02" android:layout_alignBottom="@id/TextView02"></EditT ext> <Button android:id="@+id/btnGo" android:text="Calculate" android:layout_margin="10dip" android:layout_width="wrap_content" android:layout_below="@id/txtNum2" android:layout_height="wrap_content"></Button> </RelativeLayout> result.xml PHP Code: <RelativeLayout xmlns:android="http://schemas.android.c om/apk/res/android" android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:text="A + B = " android:layout_margin="10dip" android:layout_height="wrap_content"></TextView> <EditText android:id="@+id/txtSum" android:text="" android:layout_marginRight="10dip" android:layout_marginTop="5dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:drawable/editbox_backg round" android:layout_toRightOf="@id/TextView01"></EditTex t> <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_below="@id/TextView01"

android:text="A * B = " android:layout_margin="10dip" android:layout_height="wrap_content"></TextView> <EditText android:id="@+id/txtMul" android:text="" android:layout_marginRight="10dip" android:layout_marginTop="10dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/txtSum" android:background="@android:drawable/editbox_backg round" android:layout_toRightOf="@id/TextView02"></EditTex t> <Button android:id="@+id/btnContinue" android:text="Continue" android:layout_margin="10dip" android:layout_width="wrap_content" android:layout_below="@id/txtMul" android:layout_height="wrap_content"></Button> <Button android:id="@+id/btnReset" android:text="Reset" android:layout_marginTop="10dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/txtMul" android:layout_toRightOf="@id/btnContinue"></Button > </RelativeLayout>

You might also like