You are on page 1of 17

X l bt ng b

MultiUni Trn V Tt Bnh

Thread
Thng thng dng Thread x l cc code x l nng, hoc mt thi gian c th gy chm chng trnh hoc giao din b block. Thread kh thng dng v trong Android dng lp Thread ca Java.
http://developer.android.com/reference/java/lang/Thread.html

Mc nh, mi ng dng chy trong mt process v code c thc thi trong thread chnh ca process .

Thread
Nu code x l qu lu, khng kp phn hi li cc s kin ngi dng trong 5 giy th s xut hin dialog Application is not responding v ngi dng c th force close ng dng ngay lp tc. D khng b force close th vic ng dng b lag l kh chp nhn. Tham kho 3 link sau:
http://developer.android.com/guide/practices/design/responsiveness.html http://developer.android.com/guide/practices/design/seamlessness.html http://developer.android.com/guide/practices/design/performance.html

Thread
Thread thread = new Thread() { @Override public synchronized void start() { // Khi to cc i tng cn thit ti y super.start(); } @Override public void run() { // code x l chnh ca thread trong ny super.run(); } }; thread.start(); //bt u thread

Thread
Lu :
Thread ln u thc thi gi phng thc start(), nhng ln sau ch gi phng thc run(), khng gi start() na. Cc code x l lin quan n giao din ch c x l trong thread chnh ca ng dng (v d load nh t mng v th dung thread, nhng hin th nh ln ImageView th x l trong thread chnh) Sau khi thc thi xong phng thc run(), thread khng cn active na.

Handler
Trong Android, tin vic giao tip gia 2 thread (nh cp slide trc) ta dng i tng Handler. Ngoi ra, c th dng Handler t x l mt yu cu no sau mt khong thi gian xc nh. Chi tit ti y:
http://developer.android.com/reference/android/os/Handler.ht ml

Handler
Giao tip gia 2 Thread:
Gi s trong phng thc run() ca Thread trong slide trc, ly xong i tng Bitmap v. Mun truyn i tng Bitmap cho Thread chnh hin th ln mn hnh:
Message msg = mHandler.obtainMessage(1, bitmap); mHandler.sendMessage(msg);

Trong code ca Activity (mc nh l thread chnh), ta khai bo mt i tng Handler tng tng nh sau:

Handler
Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 1) { //Hin th Bitmap mImageView.setImageBitmap((Bitmap)msg.obj); } super.handleMessage(msg); } };

Handler
Va ri, ta nh i tng mHandler ly ra mt message v gn m vo cho message , km theo i tng bitmap. Sau gi i. Message gi i s c nhn phng thc callback l handleMessage() ca i tng Handler. Handler cn c th gi message x l sau mt khong thi gian nh sn sendMessageAtTime hoc x l ti mt thi im nh sn sendMessageDelayed c th tm hiu thm trong ti liu ca lp Handler

Handler
Handler c to trong thread no th s s dng message queue ca thread . C th dng Handler nh b m giy khi chi nhc, hoc chc nng tng t Lu l nu trong message queue vn cn message th vn cn thc thi d thot khi ng dng, cn thn ch ny ch khng ng dng chy by .

AlarmManager
Dng AlarmManager thc hin ng k x l mt thao tc no ti mt thi im nht nh trong tng lai (thng l thi gian di). Nu x l trong thi gian ngn th khuyn co nn dng Handler. u im ca AlarmManager, khi n thi im c t trc, d ng dng ang khng chy vn c gi. Nu tt my th bt li cng khng cn (lu im ny)

AlarmManager
Khi to mt alarm:
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); Intent broadcastIntent = new Intent("org.multiuni.android.ACTION..."); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, broadcastIntent, PendingIntent.FLAG_CANCEL_CURRENT); am.set(AlarmManager.RTC_WAKEUP, triggerAtTime, pendingIntent);

AlarmManager
Gii thch:
Khi to mt i tng AlarmManager lm vic vi Alarm. To mt intent tn broadcastIntent, intent ny c dng gi broadcast khi n thi im nh sn. PendingIntent c khi to gm context, broadcastIntent trn v mt c bo rng nu c mt Alarm tng t th b n i v dng ci mi ny.

Sau cng, set alarm vi 3 thng s:


B m thi gian (c 4 loi, xem trong document ca AlarmManager) Thi gian chnh xc bt alarm ln. PendingIntent gi i (dng xc nh ti thi im bt alarm ln th cn pht intent no)

Tham kho thm ti y:


http://developer.android.com/reference/android/app/AlarmManag er.html

Notification
Trong nhng trng hp cc bn mun hin mt thng bo v mt s kin no cho ngi dng m khong mun nh hng n cng vic ca h hoc khng chc h c ang cm in thoi (tin nhn, cuc gi, email) Hoc bn mun hin th thng tin mt vic no ang xy ra trn in thoi v mong ngi dng bit (ang nghe nhc, ang trong cuc gi, thiu th nh) Notification

Notification
Bn c th to mt notification c m bo, rung, n led, icon Notification c 2 dng:
One time On going

Xem chi tit ti


http://developer.android.com/reference/android/app/Notification Manager.html

Notification
Code demo chi tit: ApiDemos com.example.android.apis.app StatusBarNotification.java

You might also like