6
III-Sử dụng Intent như thế nào? -Các hàm thực thi Activity -Intent tường minh thực thi Activity • Như đã trình bày ở phần II, intent có thể dùng thuộc tính phụ component để chỉ định đích danh tên lớp sẽ thực thi Activity. Để thực hiện điều này, lớp Intent cung cấp các hàm đó là setComponent(ComponentName) và setClass(Context, Class) và setClassName(Context, String) setClassName(String, String). • Chỉ được dùng để gọi các Activities trong cùng một app • VD: PHP Code: Intent intent = new Intent(); intent.setClassName("ten_package", "ten_lop_ben_tr ong_package"); startActivity(intent); www.giaotrinhcntt.com www.giaotrinhcntt.com

intent_tronglap_trinh_android_2_258.pdf

Embed Size (px)

Citation preview

  • 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);

    www.giaotrinhcntt.comwww.giaotrinhcntt.com

  • -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 Intent-filter (tt nhin nu chng ta mun gi mt built-in action th ta khng cn quan tm n vn ny). VD: PHP Code:

    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);

    www.giaotrinhcntt.comwww.giaotrinhcntt.com

  • 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

    www.giaotrinhcntt.comwww.giaotrinhcntt.com

  • input.xml PHP Code:

  • android:background="@android:drawable/editbox_backg

    round"

    android:layout_marginRight="10dip"

    android:layout_toRightOf="@id/TextView02"

    android:layout_alignBottom="@id/TextView02">

    result.xml PHP Code:

  • android:text="A * B = "

    android:layout_margin="10dip"

    android:layout_height="wrap_content">

    www.giaotrinhcntt.comwww.giaotrinhcntt.com