1 page.title=Tin trnh v Lung 2 page.tags=vng i, nn 3 4 @jd:body 5 6 <div id="qv-wrapper"> 7 <div id="qv"> 8 9 <h2>Trong ti liu ny</h2> 10 <ol> 11 <li><a href="#Processes">Tin trnh</a> 12 <ol> 13 <li><a href="#Lifecycle">Vng i tin trnh</a></li> 14 </ol> 15 </li> 16 <li><a href="#Threads">Lung</a> 17 <ol> 18 <li><a href="#WorkerThreads">Lung trnh thc hin</a></li> 19 <li><a href="#ThreadSafe">Phng php an ton vi lung</a></li> 20 </ol> 21 </li> 22 <li><a href="#IPC">Truyn thng Lin Tin trnh</a></li> 23 </ol> 24 25 </div> 26 </div> 27 28 <p>Khi mt thnh phn ng dng bt u v ng dng khng c bt k thnh phn no khc 29 ang chy, h thng Android s khi ng mt tin trnh Linux mi cho ng dng bng mt lung 30 thc thi n l. Theo mc nh, tt c thnh phn ca cng ng dng s chy trong cng tin trnh v lung 31 (c gi l lung "chnh"). Nu mt thnh phn ng dng bt u v tn ti mt tin trnh 32 cho ng dng (bi mt thnh phn khc t ng dng tn ti), khi thnh phn c 33 bt u bn trong tin trnh v s dng cng lung thc thi. Tuy nhin, bn c th sp xp cho 34 cc thnh phn khc nhau trong ng dng ca mnh chy trong cc tin trnh ring bit, v bn c th to thm 35 lung cho bt k tin trnh no.</p> 36 37 <p>Ti liu ny trnh by v cch cc tin trnh v lung vn hnh trong mt ng dng Android.</p> 38 39 40 <h2 id="Processes">Tin trnh</h2> 41 42 <p>Theo mc nh, tt c thnh phn ca cng ng dng s chy trong cng tin trnh v hu ht cc ng dng 43 s khng thay i iu ny. Tuy nhin, nu bn thy rng mnh cn kim sot mt thnh phn 44 c th thuc v mt tin trnh no , bn c th lm vy trong tp bn k khai.</p> 45 46 <p>Mc nhp bn k khai i vi mi loi phn t thnh phn—<a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code 47 <activity>}</a>, <a href="{@docRoot}guide/topics/manifest/service-element.html">{@code 48 <service>}</a>, <a href="{@docRoot}guide/topics/manifest/receiver-element.html">{@code 49 <receiver>}</a>, v <a href="{@docRoot}guide/topics/manifest/provider-element.html">{@code 50 <provider>}</a>—s h tr mt thuc tnh {@code android:process} m c th quy nh mt 51 tin trnh m thnh phn s chy trong . Bn c th t thuc tnh ny sao cho tng thnh phn chy 52 trong chnh tin trnh ca n hoc sao cho mt s thnh phn chia s mt tin trnh trong khi cc thnh phn khc th khng. Bn cng c th t 53 {@code android:process} sao cho cc thnh phn ca nhng ng dng khc nhau chy trong cng 54 tin trnh—vi iu kin rng ng dng chia s cng ID ngi dng Linux v c k bng cng cc 55 chng ch.</p> 56 57 <p>Phn t <a href="{@docRoot}guide/topics/manifest/application-element.html">{@code 58 <application>}</a> cng h tr mt thuc tnh {@code android:process}, t mt 59 gi tr mc nh p dng cho tt c thnh phn.</p> 60 61 <p>Android c th quyt nh tt mt tin trnh ti mt thi im no , khi b nh thp v theo yu cu ca cc 62 tin trnh khc ang phc v ngi dng tc th hn. Cc thnh phn 63 ng dng ang chy trong tin trnh b tt b th sau s b hy. Tin trnh c 64 khi ng li cho nhng thnh phn khi li c vic cho chng thc hin.</p> 65 66 <p>Khi quyt nh b nhng tin trnh no, h thng Android s cn nhc tm quan trng tng i so vi 67 ngi dng. V d, h thng sn sng hn khi tt mt tin trnh lu tr cc hot ng khng cn 68 hin th trn mn hnh, so vi mt tin trnh lu tr cc hot ng ang hin th. V th, quyt nh v vic c 69 chm dt mt tin trnh hay khng ph thuc vo trng thi ca cc thnh phn ang chy trong tin trnh . Cc quy tc 70 c s dng quyt nh s chm dt tin trnh no c trnh by bn di. </p> 71 72 73 <h3 id="Lifecycle">Vng i tin trnh</h3> 74 75 <p>H thng Android c gng duy tr mt tin trnh ng dng lu nht c th, nhng 76 cui cng th n cng cn loi b cc tin trnh c ly li b nh cho cc tin trnh mi hoc quan trng hn. 77 xc nh gi li nhng tin trnh no 78 v loi b nhng tin trnh no, h thng s t tng tin trnh vo mt "phn cp tm quan trng" da trn 79 nhng thnh phn ang chy trong tin trnh v trng thi ca nhng thnh phn . Nhng tin trnh c tm quan trng 80 thp nht b loi b trc, ri n nhng tin trnh c tm quan trng thp th hai, v c th tip tc, min l cn cn thit 81 khi phc ti nguyn ca h thng.</p> 82 83 <p>C nm cp trong phn cp tm quan trng. Danh sch sau trnh by cc loi 84 tin trnh khc nhau theo th t tm quan trng (tin trnh th nht l <em>quan trng nht</em> v c 85 <em>tt b sau cng</em>):</p> 86 87 <ol> 88 <li><b>Tin trnh tin cnh</b> 89 <p>Mt tin trnh c yu cu cho vic m ngi dng ang thc hin. Mt 90 tin trnh c coi l ang trong tin cnh nu bt k iu no sau y l ng:</p> 91 92 <ul> 93 <li>N lu tr mt {@link android.app.Activity} m ngi dng ang tng tc vi (phng php ca {@link 94 android.app.Activity}, {@link android.app.Activity#onResume onResume()}, c 95 gi).</li> 96 97 <li>N lu tr mt {@link android.app.Service} gn kt vi hot ng m ngi dng ang 98 tng tc vi.</li> 99 100 <li>N lu tr mt {@link android.app.Service} ang chy "trong tin cnh"—m 101 dch v gi {@link android.app.Service#startForeground startForeground()}. 102 103 <li>N lu tr mt {@link android.app.Service} m ang thc thi mt trong cc lnh 104 gi li ca vng i ca n ({@link android.app.Service#onCreate onCreate()}, {@link android.app.Service#onStart 105 onStart()}, hoc {@link android.app.Service#onDestroy onDestroy()}).</li> 106 107 <li>N lu tr mt {@link android.content.BroadcastReceiver} m ang thc thi phng php {@link 108 android.content.BroadcastReceiver#onReceive onReceive()} ca n.</li> 109 </ul> 110 111 <p>Nhn chung, ti bt k thi im xc nh no cng ch tn ti mt vi tin trnh tin cnh. Chng ch b tt b 112 nh mt gii php cui cng—nu b nh qu thp ti mc chng u khng th tip tc chy c. Nhn chung, ti thi im 113 , thit b t ti trng thi phn trang b nh, v th vic tt b mt s tin trnh tin cnh l 114 bt buc m bo giao din ngi dng c phn hi.</p></li> 115 116 <li><b>Tin trnh hin th</b> 117 <p>Mt tin trnh m khng c bt k thnh phn tin cnh no, nhng vn c th 118 nh hng ti ni dung m ngi dng nhn thy trn mn hnh. Mt tin trnh c coi l hin th nu mt trong hai 119 iu kin sau l ng:</p> 120 121 <ul> 122 <li>N lu tr mt {@link android.app.Activity} m khng nm trong tin cnh, nhng vn 123 hin th vi ngi dng (phng php {@link android.app.Activity#onPause onPause()} ca n c gi). 124 iu ny c th xy ra, v d, nu hot ng tin cnh bt u mt hp thoi, n cho php 125 hot ng trc c nhn thy pha sau n.</li> 126 127 <li>N lu tr mt {@link android.app.Service} c gn kt vi mt hot ng 128 hin th (hoc tin cnh).</li> 129 </ul> 130 131 <p>Mt tin trnh tin cnh c coi l cc k quan trng v s khng b tt b tr khi lm vy 132 l bt buc gi cho tt c tin trnh tin cnh chy. </p> 133 </li> 134 135 <li><b>Tin trnh dch v</b> 136 <p>Mt tin trnh m ang chy mt dch v c bt u bng phng php {@link 137 android.content.Context#startService startService()} v khng ri vo mt trong hai 138 th loi cao hn. Mc d tin trnh dch v khng trc tip gn vi bt k th g m ngi dng thy, chng 139 thng ang lm nhng vic m ngi dng quan tm n (chng hn nh pht nhc chy ngm hoc 140 ti xung d liu trn mng), v th h thng vn gi chng chy tr khi khng c b nh 141 duy tr chng cng vi tt c tin trnh tin cnh v hin th. </p> 142 </li> 143 144 <li><b>Tin trnh nn</b> 145 <p>Mt tin trnh lu tr mt hot ng m hin ti khng hin th vi ngi dng (phng php 146 {@link android.app.Activity#onStop onStop()} ca hot ng c gi). Nhng tin trnh ny khng c tc ng 147 trc tip ti tri nghim ngi dng, v h thng c th b chng i vo bt c lc no ly li b nh cho mt 148 tin trnh tin cnh, 149 hin th hoc dch v. Thng th c nhiu tin trnh ngm ang chy, v th chng c gi 150 trong mt danh sch LRU (t s dng gn y nht) m bo rng tin trnh vi hot ng 151 m ngi dng nhn thy gn y nht l tin trnh cui cng s b tt b. Nu mt hot ng trin khai cc phng php vng i ca n 152 ng cch, v lu trng thi hin ti ca n, vic tt b tin trnh ca hot ng s khng c nh hng c th thy c ti 153 tri nghim ngi dng, v khi ngi dng iu hng li hot ng , hot ng s khi phc 154 tt c trng thi hin th ca n. Xem ti liu <a href="{@docRoot}guide/components/activities.html#SavingActivityState">Hot ng</a> 155 bit thng tin v vic lu v khi phc trng thi.</p> 156 </li> 157 158 <li><b>Tin trnh trng</b> 159 <p>Mt tin trnh m khng gi bt k thnh phn ng dng hin hot no. L do duy nht gi cho 160 kiu tin trnh ny hot ng l nhm mc ch lu b nh n, ci thin thi gian khi ng vo ln ti khi thnh phn 161 cn chy trong n. H thng thng tt b nhng tin trnh ny cn bng ti nguyn tng th 162 ca h thng gia cc b m n tin trnh v b m n nhn lin quan.</p> 163 </li> 164 </ol> 165 166 167 <p>Android xp hng mt tin trnh mc cao nht m n c th, da vo tm quan trng ca 168 cc thnh phn ang hot ng trong tin trnh . V d, nu mt tin trnh lu gi mt dch v v hot ng 169 hin th, tin trnh s c xp hng l tin trnh hin th ch khng phi tin trnh dch v.</p> 170 171 <p>Ngoi ra, xp hng ca mt tin trnh c th tng bi cc tin trnh khc ph thuc vo 172 n—mt tin trnh m ang phc v mt tin trnh khc khng th b xp thp hn tin trnh m n 173 ang phc v. V d, nu mt trnh cung cp ni dung trong tin trnh A ang phc v mt my khch trong tin trnh B, hoc nu mt 174 dch v trong tin trnh A c gn kt vi mt thnh phn trong tin trnh B, t nht tin trnh A s lun c coi 175 l quan trng nh tin trnh B.</p> 176 177 <p>Do mt tin trnh ang chy mt dch v c xp hng cao hn mt tin trnh c cc hot ng nn, 178 mt hot ng m khi ng mt thao tc nhp gi c th lm tt vic khi ng mt <a href="{@docRoot}guide/components/services.html">dch v</a> cho thao tc , thay v 179 ch to mt lung trnh thc hin—nht l khi thao tc s c th din ra lu hn hot ng. 180 V d, mt hot ng m ang ti mt nh ln mt trang web nn bt u mt dch v thc hin 181 vic ti ln sao cho vic ti ln c th tip tc chy ngm ngay c khi ngi dng ri khi hot ng. 182 Vic s dng mt dch v s bo m rng thao tc t nht s c mc u tin nh "tin trnh dch v", 183 khng ph thuc vo iu xy ra vi hot ng. y cng chnh l l do hm nhn qung b nn 184 s dng dch v thay v ch a cc thao tc tn thi gian vo mt lung.</p> 185 186 187 188 189 <h2 id="Threads">Lung</h2> 190 191 <p>Khi mt ng dng c khi chy, h thng s to mt lung thc thi cho ng dng, 192 gi l lung "chnh." Lung ny rt quan trng bi n ph trch phn phi cc s kin ti 193 nhng widget giao din ngi dng ph hp, bao gm cc s kin v. N cng l lung m 194 trong ng dng ca bn tng tc vi cc thnh phn t b cng c UI ca Android (cc thnh phn t cc gi {@link 195 android.widget} v {@link android.view}). Nh vy, lung chnh i khi cng c gi l 196 lung UI.</p> 197 198 <p>H thng <em>khng</em> to mt lung ring cho tng thc th ca thnh phn. Tt c 199 thnh phn chy trong cng tin trnh u c khi to trong lung UI, v cc lnh gi ca h thng ti 200 tng thnh phn c phn phi t lung . H qu l cc phng php hi p li lnh 201 gi li ca h thng (chng hn nh {@link android.view.View#onKeyDown onKeyDown()} bo co hnh ng ca ngi dng 202 hoc mt phng php gi li vng i) s lun chy trong lung UI ca tin trnh.</p> 203 204 <p>V d, khi ngi dng chm vo mt nt trn mn hnh, lung UI ca ng dng ca bn s phn phi 205 s kin chm ti widget, n lt mnh, widget s t trng thi c nhn v ng mt yu cu v hiu ha ti 206 hng i s kin. Lung UI loi yu cu khi hng i v thng bo vi widget rng n nn t v li 207 .</p> 208 209 <p>Khi ng dng ca bn thc hin cng vic nng hi p tng tc ca ngi dng, m hnh lung n nht 210 ny c th dn n hiu nng km tr khi bn trin khai ng dng ca mnh mt cch ph hp. C th, nu 211 mi th ang xy ra trong lung UI, vic thc hin nhng thao tc ko di nh truy cp mng hay 212 truy vn c s d liu s chn ton b UI. Khi lung b chn, khng s kin no c th c phn phi, 213 bao gm c s kin v. T phng din ca ngi dng, ng dng 214 c v nh ang b treo. Thm ch t hn, nu lung UI b chn trong lu hn vi giy 215 (hin ti l khong 5 giy), ngi dng s c hin th hp thoi khng ph bin "<a href="http://developer.android.com/guide/practices/responsiveness.html">ng dng 216 khng phn hi</a>" (ANR). Khi , ngi dng c th quyt nh thot ng dng ca mnh v g ci t n 217 nu h khng thoi mi.</p> 218 219 <p>Ngoi ra, b cng c UI ca Android <em>khng</em> an ton vi lung. V vy, bn khng c thao tc 220 UI ca mnh t mt lung trnh thc hin—bn phi thc hin tt c thao tc i vi giao din ngi dng ca mnh t lung 221 UI. V vy, c hai quy tc n gin i vi m hnh lung n l ca Android:</p> 222 223 <ol> 224 <li>Khng c chn lung UI 225 <li>Khng c truy cp b cng c UI ca Android t bn ngoi lung UI 226 </ol> 227 228 <h3 id="WorkerThreads">Lung trnh thc hin</h3> 229 230 <p>V m hnh lung n l nu trn, iu thit yu i vi tnh phn hi ca UI 231 ng dng ca bn l bn khng c chn lung UI. Nu bn c thao tc cn thc hin 232 khng mang tnh cht tc thi, bn nn m bo thc hin chng trong cc lung ring (lung chy ngm" hoc 233 "trnh thc hin").</p> 234 235 <p>V d, bn di l mt s m cho mt i tng theo di nhp c chc nng ti xung mt hnh nh t mt lung 236 ring v hin th n trong mt {@link android.widget.ImageView}:</p> 237 238 <pre> 239 public void onClick(View v) { 240 new Thread(new Runnable() { 241 public void run() { 242 Bitmap b = loadImageFromNetwork("http://example.com/image.png"); 243 mImageView.setImageBitmap(b); 244 } 245 }).start(); 246 } 247 </pre> 248 249 <p>Thot u, iu ny c v nh din ra n tha, v n to mt lung mi x l thao tc 250 mng. Tuy nhin, n vi phm quy tc th hai ca m hnh lung n nht: <em>khng c truy cp 251 b cng c UI ca Android t bn ngoi lung UI</em>—mu ny sa i {@link 252 android.widget.ImageView} t lung trnh thc hin thay v t lung UI. iu ny c th dn n 253 hnh vi bt ng, khng c nh ngha m c th gy kh khn v tn thi gian theo di.</p> 254 255 <p> sa vn ny, Android gii thiu mt vi cch truy cp lung UI t cc lung 256 khc. Sau y l mt danh sch cc phng php c th tr gip:</p> 257 258 <ul> 259 <li>{@link android.app.Activity#runOnUiThread(java.lang.Runnable) 260 Activity.runOnUiThread(Runnable)}</li> 261 <li>{@link android.view.View#post(java.lang.Runnable) View.post(Runnable)}</li> 262 <li>{@link android.view.View#postDelayed(java.lang.Runnable, long) View.postDelayed(Runnable, 263 long)}</li> 264 </ul> 265 266 <p>V d, bn c th sa m trn bng cch s dng phng php {@link 267 android.view.View#post(java.lang.Runnable) View.post(Runnable)}:</p> 268 269 <pre> 270 public void onClick(View v) { 271 new Thread(new Runnable() { 272 public void run() { 273 final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png"); 274 mImageView.post(new Runnable() { 275 public void run() { 276 mImageView.setImageBitmap(bitmap); 277 } 278 }); 279 } 280 }).start(); 281 } 282 </pre> 283 284 <p>Gi th trin khai ny an ton vi lung: thao tc mng c thc hin t mt lung ring 285 trong khi {@link android.widget.ImageView} c thao tc t lung UI.</p> 286 287 <p>Tuy nhin, khi m s phc tp ca thao tc tng ln, kiu m ny c th b phc tp ha v 288 kh duy tr. x l nhng tng tc phc tp hn bng mt lung trnh thc hin, bn c th cn nhc 289 s dng mt {@link android.os.Handler} trong lung trnh thc hin ca mnh, x l cc th c chuyn t lung 290 UI. Mc d vy, gii php tt nht l m rng lp {@link android.os.AsyncTask}, 291 iu ny s n gin ha vic thc thi cc tc v ca lung trnh thc hin cn tng tc vi UI.</p> 292 293 294 <h4 id="AsyncTask">S dng AsyncTask</h4> 295 296 <p>{@link android.os.AsyncTask} cho php bn thc hin cng vic khng ng b trn giao din 297 ngi dng ca mnh. N thc hin cc thao tc chn trong mt lung trnh thc hin ri pht hnh kt qu trn 298 lung UI m khng yu cu bn t x l cc lung v/hoc trnh x l.</p> 299 300 <p> s dng n, bn phi to lp con {@link android.os.AsyncTask} v trin khai phng php gi li {@link 301 android.os.AsyncTask#doInBackground doInBackground()}, phng php ny chy trong mt tp hp 302 cc lung chy ngm. cp nht UI ca mnh, bn nn trin khai {@link 303 android.os.AsyncTask#onPostExecute onPostExecute()}, n s mang li kt qu t {@link 304 android.os.AsyncTask#doInBackground doInBackground()} v chy trong lung UI, v th bn c th nng cp 305 UI ca mnh mt cch an ton. Sau , bn c th chy tc v bng cch gi {@link android.os.AsyncTask#execute execute()} 306 t lung UI.</p> 307 308 <p>V d, bn c th trin khai v d trc bng cch s dng {@link android.os.AsyncTask} theo 309 cch ny:</p> 310 311 <pre> 312 public void onClick(View v) { 313 new DownloadImageTask().execute("http://example.com/image.png"); 314 } 315 316 private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 317 /** The system calls this to perform work in a worker thread and 318 * delivers it the parameters given to AsyncTask.execute() */ 319 protected Bitmap doInBackground(String... urls) { 320 return loadImageFromNetwork(urls[0]); 321 } 322 323 /** The system calls this to perform work in the UI thread and delivers 324 * the result from doInBackground() */ 325 protected void onPostExecute(Bitmap result) { 326 mImageView.setImageBitmap(result); 327 } 328 } 329 </pre> 330 331 <p>Lc ny, UI an ton v m n gin hn, v n tch ring cng vic thnh 332 phn s c thc hin trn mt lung trnh thc hin v phn s c thc hin trn lung UI.</p> 333 334 <p>Bn nn c ti liu tham kho {@link android.os.AsyncTask} hiu y v 335 cch s dng lp ny, nhng sau y l phn trnh by tng quan nhanh v hot ng ca n:</p> 336 337 <ul> 338 <li>Bn c th quy nh loi tham s, cc gi tr tin , v gi tr 339 cui cng ca tc v, bng cch s dng cc kiu chung</li> 340 <li>Phng php {@link android.os.AsyncTask#doInBackground doInBackground()} s t ng thc thi 341 trn mt lung trnh thc hin</li> 342 <li>{@link android.os.AsyncTask#onPreExecute onPreExecute()}, {@link 343 android.os.AsyncTask#onPostExecute onPostExecute()}, v {@link 344 android.os.AsyncTask#onProgressUpdate onProgressUpdate()} u c gi ra trn lung UI</li> 345 <li>Gi tr c tr v bi {@link android.os.AsyncTask#doInBackground doInBackground()} c gi ti 346 {@link android.os.AsyncTask#onPostExecute onPostExecute()}</li> 347 <li>Bn c th gi {@link android.os.AsyncTask#publishProgress publishProgress()} vo bt c lc no trong {@link 348 android.os.AsyncTask#doInBackground doInBackground()} thc thi {@link 349 android.os.AsyncTask#onProgressUpdate onProgressUpdate()} trn lung UI</li> 350 <li>Bn c th hy b tc v vo bt c lc no t bt k lung no</li> 351 </ul> 352 353 <p class="caution"><strong>Ch :</strong> Mt vn khc m bn c th gp phi khi s dng mt lung 354 trnh thc hin l nhng ln khi ng li bt ng trong hot ng ca bn do mt <a href="{@docRoot}guide/topics/resources/runtime-changes.html">thay i trong cu hnh thi gian chy</a> 355 (chng hn nh khi ngi dng thay i hng mn hnh), iu ny c th lm hng lung trnh thc hin ca bn. 356 xem cch bn c th duy tr tc v ca mnh khi din ra mt trong nhng ln khi ng li ny v cch hy b tc v cho ph hp 357 khi hot ng b hy, hy xem m ngun cho ng dng mu <a href="http://code.google.com/p/shelves/">Shelves</a>.</p> 358 359 360 <h3 id="ThreadSafe">Phng php an ton vi lung</h3> 361 362 <p> Trong mt s tnh hung, cc phng php bn trin khai c th c gi ra t nhiu hn mt lung, v v th 363 phi c ghi sao cho an ton vi lung. </p> 364 365 <p>iu ny ch yu ng vi cc phng php m c th c gi t xa—chng hn nh cc phng php trong mt <a href="{@docRoot}guide/components/bound-services.html">dch v gn kt</a>. Khi mt lnh gi trn mt 366 phng php c trin khai trong mt {@link android.os.IBinder} khi u trong cng tin trnh m 367 {@link android.os.IBinder IBinder} ang chy, phng php s c thc thi trong lung ca hm gi. 368 Tuy nhin, khi lnh gi khi u trong mt tin trnh khc, phng php s c thc thi trong mt lung c chn t 369 mt tp hp cc lung m h thng duy tr trong cng tin trnh nh {@link android.os.IBinder 370 IBinder} (n khng c thc thi trong lung UI ca tin trnh). V d, trong khi phng php 371 {@link android.app.Service#onBind onBind()} ca dch v s c gi t lung UI ca tin trnh 372 ca dch v, cc phng php c trin khai trong i tng m {@link android.app.Service#onBind 373 onBind()} tr v (v d, mt lp con trin khai cc phng php RPC) s c gi t cc lung 374 trong tp hp. V mt dch v c th c nhiu hn mt my khch, nhiu hn mt lung tp hp c th s dng 375 cng mt phng php {@link android.os.IBinder IBinder} ti cng mt thi im. V th, cc phng php {@link android.os.IBinder 376 IBinder} phi c trin khai sao cho an ton vi lung.</p> 377 378 <p> Tng t, mt trnh cung cp ni dung c th nhn cc yu cu d liu khi ngun trong cc tin trnh khc. 379 Mc d cc lp {@link android.content.ContentResolver} v {@link android.content.ContentProvider} 380 n i chi tit v cch truyn thng lin tin trnh c qun l, cc phng php {@link 381 android.content.ContentProvider} hi p nhng yu cu —cc phng php {@link 382 android.content.ContentProvider#query query()}, {@link android.content.ContentProvider#insert 383 insert()}, {@link android.content.ContentProvider#delete delete()}, {@link 384 android.content.ContentProvider#update update()}, v {@link android.content.ContentProvider#getType 385 getType()}—c gi t mt tp hp lung trong tin trnh ca trnh cung cp ni dung, ch khng phi lung 386 UI cho tin trnh . V nhng phng php ny c th c gi t bt k s lng lung no ti 387 cng thi im, chng cng phi c trin khai sao cho an ton vi lung. </p> 388 389 390 <h2 id="IPC">Truyn thng Lin Tin trnh</h2> 391 392 <p>Android cung cp mt c ch cho truyn thng lin tin trnh (IPC) bng cch s dng cc lnh gi th tc t xa 393 (RPC), trong mt phng php c gi bi mt hot ng hoc thnh phn ng dng khc, nhng c thc thi 394 t xa (trong mt tin trnh khc), vi bt k kt qu no c tr v 395 hm gi. iu ny i hi vic phn tch mt lnh gi phng php v d liu ca n v cp m h iu hnh 396 c th hiu c, truyn pht n t tin trnh v khong trng a ch cc b n tin trnh v 397 khong trng a ch t xa, sau t hp li v pht hnh li lnh gi . Sau , cc gi tr tr v c 398 pht theo hng ngc li. Android cung cp tt c m thc hin nhng giao tc 399 IPC ny, v th bn c th tp trung vo vic nh ngha v trin khai giao din lp trnh RPC. </p> 400 401 <p> thc hin IPC, ng dng ca bn phi lin kt vi mt dch v, bng cch s dng {@link 402 android.content.Context#bindService bindService()}. bit thm thng tin, hy xem hng dn cho nh pht trin <a href="{@docRoot}guide/components/services.html">Dch v</a>.</p> 403 404 405 <!-- 406 <h2>Beginner's Path</h2> 407 408 <p>For information about how to perform work in the background for an indefinite period of time 409 (without a user interface), continue with the <b><a 410 href="{@docRoot}guide/components/services.html">Services</a></b> document.</p> 411 --> 412