1 page.title=Dch v 2 @jd:body 3 4 <div id="qv-wrapper"> 5 <ol id="qv"> 6 <h2>Trong ti liu ny</h2> 7 <ol> 8 <li><a href="#Basics">Ni dung C bn</a></li> 9 <ol> 10 <li><a href="#Declaring">Khai bo mt dch v trong bn k khai</a></li> 11 </ol> 12 <li><a href="#CreatingAService">To mt Dch v c Bt u</a> 13 <ol> 14 <li><a href="#ExtendingIntentService">M rng lp IntentService</a></li> 15 <li><a href="#ExtendingService">M rng lp Dch v</a></li> 16 <li><a href="#StartingAService">Bt u mt dch v</a></li> 17 <li><a href="#Stopping">Dng mt dch v</a></li> 18 </ol> 19 </li> 20 <li><a href="#CreatingBoundService">To mt Dch v Gn kt</a></li> 21 <li><a href="#Notifications">Gi Thng bo ti Ngi dng</a></li> 22 <li><a href="#Foreground">Chy mt Dch v trong Tin cnh</a></li> 23 <li><a href="#Lifecycle">Qun l Vng i ca mt Dch v</a> 24 <ol> 25 <li><a href="#LifecycleCallbacks">Trin khai gi li vng i</a></li> 26 </ol> 27 </li> 28 </ol> 29 30 <h2>Lp kha</h2> 31 <ol> 32 <li>{@link android.app.Service}</li> 33 <li>{@link android.app.IntentService}</li> 34 </ol> 35 36 <h2>Mu</h2> 37 <ol> 38 <li><a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/ServiceStartArguments.html">{@code 39 ServiceStartArguments}</a></li> 40 <li><a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/app/LocalService.html">{@code 41 LocalService}</a></li> 42 </ol> 43 44 <h2>Xem thm</h2> 45 <ol> 46 <li><a href="{@docRoot}guide/components/bound-services.html">Dch v Gn kt</a></li> 47 </ol> 48 49 </div> 50 51 52 <p>{@link android.app.Service} l mt thnh phn ng dng c kh nng thc hin 53 cc thao tc chy ko di trong nn v khng cung cp giao din ngi dng. Mt 54 thnh phn ng dng khc c th bt u mt dch v v n s tip tc chy ngm ngay c khi ngi dng 55 chuyn sang mt ng dng khc. Ngoi ra, mt thnh phn c th gn kt vi mt dch v 56 tng tc vi n v thm ch thc hin truyn thng lin tin trnh (IPC). V d, mt dch v c th 57 x l cc giao dch mng, pht nhc, thc hin I/O tp, hoc tng tc vi mt trnh cung cp ni dung, tt c 58 u xut pht t nn.</p> 59 60 <p>V c bn, mt dch v c th c hai dng:</p> 61 62 <dl> 63 <dt>c bt u</dt> 64 <dd>Dch v c dng "c bt u" khi mt thnh phn ng dng (chng hn nh mt hot ng) bt u n bng cch 65 gi {@link android.content.Context#startService startService()}. Sau khi c bt u, dch v 66 c th chy ngm v thi hn, ngay c khi thnh phn bt u n b hy. Thng thng, 67 dch v c bt u s thc hin mt thao tc n l v khng tr v kt qu cho hm gi. 68 V d, n c th ti xung hoc ti ln mt tp thng qua mng. Khi thao tc c hon thnh, dch v 69 t n s dng li.</dd> 70 <dt>Gn kt</dt> 71 <dd>Dch v c dng "gn kt" khi mt thnh phn ng dng gn kt vi n bng cch gi {@link 72 android.content.Context#bindService bindService()}. Dch v gn kt s a ra 73 mt giao din my khch-my ch cho php cc thnh phn tng tc vi dch v, gi yu cu, nhn kt qu, v thm ch 74 lm vy thng qua truyn thng lin tin trnh (IPC). Dch v gn kt ch chy trong khi 75 mt thnh phn ng dng khc c gn kt vi n. Nhiu thnh phn c th gn kt cng lc vi dch v, 76 nhng khi tt c b b gn kt th dch v s b hy.</dd> 77 </dl> 78 79 <p>Mc d ti liu ny thng cp ti hai loi dch v ring r, dch v 80 ca bn c th hot ng theo c hai cch—n c th c bt u ( chy v thi hn) v cng cho php gn kt. 81 n gin l vn bn c trin khai mt cp phng php gi li hay khng: {@link 82 android.app.Service#onStartCommand onStartCommand()} cho php thnh phn bt u n v {@link 83 android.app.Service#onBind onBind()} cho php n gn kt.</p> 84 85 <p>Khng ph thuc vo vic ng dng ca bn c bt u, gn kt, hay c hai, bt k thnh phn ng dng no 86 cng c th s dng dch v (thm ch t mt ng dng ring bit), ging nh cch m bt k thnh phn no cng c th s dng 87 mt hot ng—bng cch bt u n bng mt {@link android.content.Intent}. Tuy nhin, bn c th khai bo 88 dch v l ring t trong tp bn k khai, v chn truy cp t cc ng dng khc. iu ny 89 c trnh by k hn trong phn v <a href="#Declaring">Khai bo dch v trong 90 bn k khai</a>.</p> 91 92 <p class="caution"><strong>Ch :</strong> Mt dch v chy trong 93 lung chnh ca tin trnh lu tr ca n—dch v <strong>khng</strong> to lung ca chnh n 94 v <strong>khng</strong> chy trong mt tin trnh ring bit (tr khi bn quy nh khc). iu ny c ngha 95 l, nu dch v ca bn nh thc hin bt k cng vic nng no i vi CPU hay chn cc thao tc (chng hn nh pht li MP3 96 hay kt ni mng), bn nn to mt lung mi bn trong dch v thc hin cng vic . Bng cch s dng 97 mt lung ring bit, bn s gim ri ro gp li ng dng Khng Hi p (ANR) v lung chnh ca ng dng c th 98 vn dnh ring cho tng tc gia ngi dng vi cc hot ng ca bn.</p> 99 100 101 <h2 id="Basics">Ni dung C bn</h2> 102 103 <div class="sidebox-wrapper"> 104 <div class="sidebox"> 105 <h3>Bn nn s dng dch v hay lung?</h3> 106 <p>Dch v n thun l mt thnh phn c th chy ngm ngay c khi ngi dng khng 107 ang tng tc vi ng dng ca bn. V th, bn ch nn to mt dch v nu l iu bn 108 cn.</p> 109 <p>Nu bn cn thc hin cng vic bn ngoi lung chnh ca mnh, nhng ch trong khi ngi dng ang tng tc vi 110 ng dng ca bn, th thay vo , bn nn to mt lung mi ch khng phi mt dch v. V 111 d, nu bn mun pht mt bn nhc, nhng ch trong khi hot ng ca bn ang chy, bn c th to 112 mt lung trong {@link android.app.Activity#onCreate onCreate()}, bt u chy n trong {@link 113 android.app.Activity#onStart onStart()}, ri dng n trong {@link android.app.Activity#onStop 114 onStop()}. Cng xem xt vic s dng {@link android.os.AsyncTask} hoc {@link android.os.HandlerThread}, 115 thay v s dng lp {@link java.lang.Thread} truyn thng. Xem ti liu <a href="{@docRoot}guide/components/processes-and-threads.html#Threads">Tin trnh v 116 Lung</a> bit thm thng tin v lung.</p> 117 <p>Hy nh rng nu bn s dng mt dch v, n vn chy trong lung chnh ca ng dng ca bn theo 118 mc nh, v th bn vn nn to mt lung mi trong dch v nu n thc hin cc thao tc tng cng hoc 119 chn.</p> 120 </div> 121 </div> 122 123 <p> to mt dch v, bn phi to mt lp con ca {@link android.app.Service} (hoc mt 124 trong cc lp con hin ti ca n). Trong trin khai ca mnh, bn cn khng ch mt s phng php gi li c chc nng 125 x l nhng kha cnh chnh trong vng i ca dch v v cung cp mt c ch cc thnh phn gn kt vi 126 dch v , nu ph hp. Nhng phng php gi li quan trng nht m bn nn khng ch l:</p> 127 128 <dl> 129 <dt>{@link android.app.Service#onStartCommand onStartCommand()}</dt> 130 <dd>H thng s gi phng php ny khi mt thnh phn khc, chng hn nh mt hot ng, 131 yu cu dch v phi c bt u, bng cch gi {@link android.content.Context#startService 132 startService()}. Sau khi phng php ny thc thi, dch v s c bt u v c th chy v thi hn trong 133 nn. Nu bn trin khai iu ny, bn c trch nhim dng dch v khi 134 cng vic ca n c hon thnh, bng cch gi {@link android.app.Service#stopSelf stopSelf()} hoc {@link 135 android.content.Context#stopService stopService()}. (Nu ch mun cung cp kh nng gn kt, bn khng 136 cn trin khai phng php ny.)</dd> 137 <dt>{@link android.app.Service#onBind onBind()}</dt> 138 <dd>H thng s gi phng php ny khi mt thnh phn khc mun gn kt vi 139 dch v (chng hn nh thc hin RPC), bng cch gi {@link android.content.Context#bindService 140 bindService()}. Trong trin khai phng php ny ca mnh, bn phi cung cp mt giao din m cc my khch 141 s dng giao tip vi dch v, bng cch tr v {@link android.os.IBinder}. Bn phi lun 142 trin khai phng php ny, nhng nu bn khng mun cho php gn kt th bn nn tr v rng.</dd> 143 <dt>{@link android.app.Service#onCreate()}</dt> 144 <dd>H thng s gi phng php ny khi dch v c to lp ln u, thc hin quy trnh thit lp mt ln 145 (trc khi n c th gi hoc {@link android.app.Service#onStartCommand onStartCommand()} hoc 146 {@link android.app.Service#onBind onBind()}). Nu dch v ang chy, phng php ny s khng c 147 gi.</dd> 148 <dt>{@link android.app.Service#onDestroy()}</dt> 149 <dd>H thng s gi phng php ny khi dch v khng cn c s dng v ang b hy. 150 Dch v ca bn s trin khai phng php ny dn dp mi ti nguyn nh lung, i tng theo di 151 c ng k, hm nhn, v.v... y l lnh gi cui cng m dch v nhn c.</dd> 152 </dl> 153 154 <p>Nu mt thnh phn bt u dch v bng cch gi {@link 155 android.content.Context#startService startService()} (kt qu l mt lnh gi ti {@link 156 android.app.Service#onStartCommand onStartCommand()}), khi dch v 157 s vn chy ti khi t n dng bng {@link android.app.Service#stopSelf()} hoc mt 158 thnh phn khc dng n bng cch gi {@link android.content.Context#stopService stopService()}.</p> 159 160 <p>Nu mt thnh phn gi 161 {@link android.content.Context#bindService bindService()} to dch v (v {@link 162 android.app.Service#onStartCommand onStartCommand()} <em>khng</em> c gi), khi dch v s ch chy 163 khi no m thnh phn cn gn kt vi n. Sau khi dch v c b gn kt khi tt c my khch, h thng 164 s hy n.</p> 165 166 <p>H thng Android s buc dng mt dch v ch khi b nh thp v n phi khi phc ti nguyn 167 ca h thng cho hot ng c tiu im ca ngi dng. Nu dch v gn kt vi mt hot ng m c tiu im 168 ca ngi dng, khi s c t kh nng n s b tt b hn, v nu dch v c khai bo l <a href="#Foreground">chy trong tin cnh</a> ( cp sau), khi n s hu nh khng bao gi b tt b. 169 Mt khc, nu dch v c bt u v chy trong thi gian di, h thng s h thp v tr ca n 170 trong danh sch tc v chy ngm qua thi gian v dch v s rt c th b 171 tt b—nu dch v ca bn c bt u, khi bn phi thit k n 172 x l vic khi ng li do h thng mt cch uyn chuyn. Nu h thng tt b dch v ca bn, n s khi ng li dch v ngay khi ti nguyn 173 c sn tr li (mc d iu ny cng ph thuc vo gi tr m bn tr v t {@link 174 android.app.Service#onStartCommand onStartCommand()}, vn ny s c bn sau). bit thm thng tin 175 v thi im m h thng c th hy mt dch v, hy xem ti liu <a href="{@docRoot}guide/components/processes-and-threads.html">Tin trnh v Lung</a> 176 .</p> 177 178 <p>Trong nhng phn sau, bn s thy cch bn c th to tng loi dch v v cch s dng 179 n t cc thnh phn ng dng khc.</p> 180 181 182 183 <h3 id="Declaring">Khai bo mt dch v trong bn k khai</h3> 184 185 <p>Ging nh hot ng (v cc thnh phn khc), bn phi khai bo tt c dch v trong tp bn k khai 186 ca ng dng ca mnh.</p> 187 188 <p> khai bo dch v ca bn, hy thm mt phn t <a href="{@docRoot}guide/topics/manifest/service-element.html">{@code <service>}</a> lm 189 con ca phn t <a href="{@docRoot}guide/topics/manifest/application-element.html">{@code <application>}</a> 190 . V d:</p> 191 192 <pre> 193 <manifest ... > 194 ... 195 <application ... > 196 <service android:name=".ExampleService" /> 197 ... 198 </application> 199 </manifest> 200 </pre> 201 202 <p>Xem tham chiu phn t <a href="{@docRoot}guide/topics/manifest/service-element.html">{@code <service>}</a> 203 bit thm thng tin v vic khai bo dch v ca bn trong bn k khai.</p> 204 205 <p>C cc thuc tnh khc m bn c th bao gm trong phn t <a href="{@docRoot}guide/topics/manifest/service-element.html">{@code <service>}</a> 206 nh ngha cc tnh cht chng hn nh nhng quyn cn bt u dch v v tin trnh m 207 dch v s chy trong . Thuc tnh <a href="{@docRoot}guide/topics/manifest/service-element.html#nm">{@code android:name}</a> 208 l thuc tnh bt buc duy nht—n quy nh tn lp ca dch v. Mt khi 209 bn pht hnh ng dng ca mnh, bn khng nn thay i tn ny, v nu bn lm vy, bn s gp ri ro lm gy 210 m do s ph thuc vo cc nh biu th bt u hoc gn kt dch v (c bi ng blog, <a href="http://android-developers.blogspot.com/2011/06/things-that-cannot-change.html">Nhng iu 211 Khng Thay i c</a>). 212 213 <p> m bo ng dng ca bn c bo mt, <strong>lun s dng mt nh biu th khi bt u hoc gn kt 214 {@link android.app.Service}</strong> ca bn v khng c khai bo b lc nh cho dch v. Nu 215 iu trng yu l bn phi cho php mt cht khng r rng v dch v no s bt u, bn c th 216 cung cp b lc nh cho dch v ca mnh v loi b tn thnh phn khi {@link 217 android.content.Intent}, nhng sau bn c th t gi cho nh bng {@link 218 android.content.Intent#setPackage setPackage()}, iu ny cung cp s khng r rng va cho 219 dch v mc tiu .</p> 220 221 <p>Ngoi ra, bn c th m bo rng dch v ca mnh ch sn c cho ng dng ca bn bng cch 222 a vo thuc tnh <a href="{@docRoot}guide/topics/manifest/service-element.html#exported">{@code android:exported}</a> 223 v t n thnh {@code "false"}. iu ny s dng vic cc ng dng khc bt u 224 dch v ca bn, ngay c khi s dng mt nh biu th.</p> 225 226 227 228 229 <h2 id="CreatingStartedService">To mt Dch v c Bt u</h2> 230 231 <p>Dch v c bt u l dch v m mt thnh phn khc bt u bng cch gi {@link 232 android.content.Context#startService startService()}, kt qu l mt lnh gi ti phng php 233 {@link android.app.Service#onStartCommand onStartCommand()} ca dch v.</p> 234 235 <p>Khi mt dch v c bt u, n c mt vng i c lp vi 236 thnh phn bt u n v dch v c th chy ngm v thi hn, ngay c khi 237 thnh phn bt u n b hy. Nh vy, dch v s t dng khi lm xong cng vic ca n 238 bng cch gi {@link android.app.Service#stopSelf stopSelf()}, hoc mt thnh phn khc c th dng n 239 bng cch gi {@link android.content.Context#stopService stopService()}.</p> 240 241 <p>Mt thnh phn ng dng chng hn nh mt hot ng c th bt u dch v bng cch gi {@link 242 android.content.Context#startService startService()} v chuyn mt {@link android.content.Intent} 243 trong quy nh dch v v bao gm bt k d liu no cho dch v s dng. Dch v s nhn 244 {@link android.content.Intent} ny trong phng php {@link android.app.Service#onStartCommand 245 onStartCommand()}.</p> 246 247 <p>V d, gi s mt hot ng cn lu mt s d liu vo c s d liu trc tuyn. Hot ng c th 248 bt u mt dch v ng hnh v truyn cho n d liu lu bng cch chuyn mt nh ti {@link 249 android.content.Context#startService startService()}. Dch v s nhn nh trong {@link 250 android.app.Service#onStartCommand onStartCommand()}, kt ni vi Internet v thc hin 251 giao tc c s d liu. Khi giao tc c thc hin, dch v s t dng li v n b 252 hy.</p> 253 254 <p class="caution"><strong>Ch :</strong> Mt dch v s chy trong cng tin trnh nh ng dng 255 m n c khai bo trong v trong lung chnh ca ng dng theo mc nh. V vy, nu dch v ca bn 256 thc hin cc thao tc tng cng hoc chn trong khi ngi dng tng tc vi mt hot ng t cng 257 ng dng, dch v s lm chm hiu nng ca hot ng. trnh tc ng ti hiu nng ca 258 ng dng, bn nn bt u mt lung mi bn trong dch v.</p> 259 260 <p>Thng thng, c hai lp m bn c th m rng to mt dch v c bt u:</p> 261 <dl> 262 <dt>{@link android.app.Service}</dt> 263 <dd>y l lp c bn cho tt c dch v. Khi bn m rng lp ny, iu quan trng l 264 bn to mt lung mi thc hin tt c cng vic ca dch v trong , do dch v s dng lung chnh 265 ca ng dng ca bn, theo mc nh, iu ny c th lm chm hiu nng ca bt k hot ng no m ng dng 266 ca bn ang chy.</dd> 267 <dt>{@link android.app.IntentService}</dt> 268 <dd>y l mt lp con ca {@link android.app.Service} c chc nng s dng mt lung trnh thc hin x l tt c 269 yu cu bt u mt cch ln lt. y l la chn tt nht nu bn khng yu cu dch v ca mnh 270 x l ng thi nhiu yu cu. Tt c nhng g bn cn lm l trin khai {@link 271 android.app.IntentService#onHandleIntent onHandleIntent()}, n s nhn nh cho mi 272 yu cu bt u bn c th thc hin cng vic chy ngm.</dd> 273 </dl> 274 275 <p>Cc phn sau m t cch bn c th trin khai dch v ca mnh bng cch s dng mt trong cc cch cho nhng lp 276 ny.</p> 277 278 279 <h3 id="ExtendingIntentService">M rng lp IntentService</h3> 280 281 <p>V phn ln cc dch v c bt u khng cn x l nhiu yu cu mt cch ng thi 282 (iu ny thc s c th l mt kch bn to a lung nguy him), c l tt nht l nu bn 283 trin khai dch v ca mnh bng cch s dng lp {@link android.app.IntentService}.</p> 284 285 <p>{@link android.app.IntentService} lm iu sau y:</p> 286 287 <ul> 288 <li>To mt lung trnh thc hin mc nh thc thi tt c nh c chuyn ti {@link 289 android.app.Service#onStartCommand onStartCommand()} tch ring vi lung 290 chnh ca ng dng ca bn.</li> 291 <li>To mt hng i cng vic chuyn ln lt tng nh ti trin khai {@link 292 android.app.IntentService#onHandleIntent onHandleIntent()} ca bn, v th bn khng bao gi phi 293 lo lng v vn to a lung.</li> 294 <li>Dng dch v sau khi tt c yu cu bt u u c x l, v th bn khng bao gi phi gi 295 {@link android.app.Service#stopSelf}.</li> 296 <li>Cung cp trin khai mc nh ca {@link android.app.IntentService#onBind onBind()} m 297 tr v rng.</li> 298 <li>Cung cp trin khai mc nh ca {@link android.app.IntentService#onStartCommand 299 onStartCommand()} m gi nh ti hng i cng vic ri ti trin khai {@link 300 android.app.IntentService#onHandleIntent onHandleIntent()} ca bn.</li> 301 </ul> 302 303 <p>Tt c u ni ln mt thc t rng tt c nhng vic bn cn lm l trin khai {@link 304 android.app.IntentService#onHandleIntent onHandleIntent()} thc hin cng vic m 305 my khch cung cp. (Mc d bn cng cn cung cp mt hm dng nh cho dch v.)</p> 306 307 <p>Sau y l v d v trin khai {@link android.app.IntentService}:</p> 308 309 <pre> 310 public class HelloIntentService extends IntentService { 311 312 /** 313 * A constructor is required, and must call the super {@link android.app.IntentService#IntentService} 314 * constructor with a name for the worker thread. 315 */ 316 public HelloIntentService() { 317 super("HelloIntentService"); 318 } 319 320 /** 321 * The IntentService calls this method from the default worker thread with 322 * the intent that started the service. When this method returns, IntentService 323 * stops the service, as appropriate. 324 */ 325 @Override 326 protected void onHandleIntent(Intent intent) { 327 // Normally we would do some work here, like download a file. 328 // For our sample, we just sleep for 5 seconds. 329 long endTime = System.currentTimeMillis() + 5*1000; 330 while (System.currentTimeMillis() < endTime) { 331 synchronized (this) { 332 try { 333 wait(endTime - System.currentTimeMillis()); 334 } catch (Exception e) { 335 } 336 } 337 } 338 } 339 } 340 </pre> 341 342 <p> l tt c nhng g bn cn: mt hm dng v trin khai {@link 343 android.app.IntentService#onHandleIntent onHandleIntent()}.</p> 344 345 <p>Nu bn quyt nh cng khng ch cc phng php gi li khc, chng hn nh {@link 346 android.app.IntentService#onCreate onCreate()}, {@link 347 android.app.IntentService#onStartCommand onStartCommand()}, hoc {@link 348 android.app.IntentService#onDestroy onDestroy()}, hy nh gi ra siu trin khai, sao 349 cho {@link android.app.IntentService} c th x l hp l vng i ca lung trnh thc hin.</p> 350 351 <p>V d, {@link android.app.IntentService#onStartCommand onStartCommand()} phi tr v 352 trin khai mc nh ( l cch m nh c chuyn ti {@link 353 android.app.IntentService#onHandleIntent onHandleIntent()}):</p> 354 355 <pre> 356 @Override 357 public int onStartCommand(Intent intent, int flags, int startId) { 358 Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show(); 359 return super.onStartCommand(intent,flags,startId); 360 } 361 </pre> 362 363 <p>Bn cnh {@link android.app.IntentService#onHandleIntent onHandleIntent()}, phng php duy nht m 364 t bn khng cn gi siu lp l {@link android.app.IntentService#onBind 365 onBind()} (nhng bn ch cn trin khai iu nu dch v ca bn cho php gn kt).</p> 366 367 <p>Trong phn tip theo, bn s thy cch m cng loi dch v c trin khai khi m rng 368 lp {@link android.app.Service} c s, n c nhiu m hn nhng c th 369 ph hp nu bn cn x l cc yu cu bt u ng thi.</p> 370 371 372 <h3 id="ExtendingService">M rng lp Dch v</h3> 373 374 <p>Nh bn thy trong phn trc, s dng {@link android.app.IntentService} gip vic 375 trin khai mt dch v c bt u ca bn tr nn rt n gin. Tuy nhin, nu bn cn dch v ca mnh 376 thc hin to a lung (thay v x l cc yu cu bt u thng qua mt hng i cng vic), khi bn 377 c th m rng lp {@link android.app.Service} x l tng nh.</p> 378 379 <p> so snh, on m mu sau l trin khai lp {@link 380 android.app.Service} m thc hin chnh xc cng cng vic nh v d bn trn bng cch s dng {@link 381 android.app.IntentService}. C th, i vi mi yu cu bt u, n s s dng mt lung trnh thc hin thc hin 382 cng vic v ch x l ln lt tng yu cu mt.</p> 383 384 <pre> 385 public class HelloService extends Service { 386 private Looper mServiceLooper; 387 private ServiceHandler mServiceHandler; 388 389 // Handler that receives messages from the thread 390 private final class ServiceHandler extends Handler { 391 public ServiceHandler(Looper looper) { 392 super(looper); 393 } 394 @Override 395 public void handleMessage(Message msg) { 396 // Normally we would do some work here, like download a file. 397 // For our sample, we just sleep for 5 seconds. 398 long endTime = System.currentTimeMillis() + 5*1000; 399 while (System.currentTimeMillis() < endTime) { 400 synchronized (this) { 401 try { 402 wait(endTime - System.currentTimeMillis()); 403 } catch (Exception e) { 404 } 405 } 406 } 407 // Stop the service using the startId, so that we don't stop 408 // the service in the middle of handling another job 409 stopSelf(msg.arg1); 410 } 411 } 412 413 @Override 414 public void onCreate() { 415 // Start up the thread running the service. Note that we create a 416 // separate thread because the service normally runs in the process's 417 // main thread, which we don't want to block. We also make it 418 // background priority so CPU-intensive work will not disrupt our UI. 419 HandlerThread thread = new HandlerThread("ServiceStartArguments", 420 Process.THREAD_PRIORITY_BACKGROUND); 421 thread.start(); 422 423 // Get the HandlerThread's Looper and use it for our Handler 424 mServiceLooper = thread.getLooper(); 425 mServiceHandler = new ServiceHandler(mServiceLooper); 426 } 427 428 @Override 429 public int onStartCommand(Intent intent, int flags, int startId) { 430 Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show(); 431 432 // For each start request, send a message to start a job and deliver the 433 // start ID so we know which request we're stopping when we finish the job 434 Message msg = mServiceHandler.obtainMessage(); 435 msg.arg1 = startId; 436 mServiceHandler.sendMessage(msg); 437 438 // If we get killed, after returning from here, restart 439 return START_STICKY; 440 } 441 442 @Override 443 public IBinder onBind(Intent intent) { 444 // We don't provide binding, so return null 445 return null; 446 } 447 448 @Override 449 public void onDestroy() { 450 Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show(); 451 } 452 } 453 </pre> 454 455 <p>Nh bn c th thy, c nhiu vic hn nhiu so vi vic s dng {@link android.app.IntentService}.</p> 456 457 <p>Tuy nhin, do bn t mnh x l tng lnh gi n {@link android.app.Service#onStartCommand 458 onStartCommand()}, bn c th thc hin nhiu yu cu mt cch ng thi. khng phi l vic 459 m v d ny lm, nhng nu l vic bn mun, vy bn c th to mt lung mi cho tng 460 yu cu v ngay lp tc tr chng v (thay v i ti khi yu cu trc hon thnh).</p> 461 462 <p> rng phng php {@link android.app.Service#onStartCommand onStartCommand()} phi tr v mt 463 s nguyn. S nguyn l mt gi tr m t cch h thng nn tip tc dch v trong 464 trng hp h thng tt b n (nh c cp trn, trin khai mc nh cho {@link 465 android.app.IntentService} s x l iu ny cho bn d bn c th sa i n). Gi tr tr v 466 t {@link android.app.Service#onStartCommand onStartCommand()} phi l mt trong cc 467 hng s sau:</p> 468 469 <dl> 470 <dt>{@link android.app.Service#START_NOT_STICKY}</dt> 471 <dd>Nu h thng tt b dch v sau khi {@link android.app.Service#onStartCommand 472 onStartCommand()} tr v, <em>khng</em> c to li dch v , tr khi c cc nh 473 ang ch c chuyn. y l la chn an ton nht trnh chy dch v ca bn khi khng cn thit 474 v khi ng dng ca bn c th n thun khi ng li bt k cng vic cha hon thnh no.</dd> 475 <dt>{@link android.app.Service#START_STICKY}</dt> 476 <dd>Nu h thng tt b dch v sau khi {@link android.app.Service#onStartCommand 477 onStartCommand()} tr v, hy to li dch v v gi {@link 478 android.app.Service#onStartCommand onStartCommand()}, nhng <em>khng</em> chuyn li nh cui cng. 479 Thay vo , h thng s gi {@link android.app.Service#onStartCommand onStartCommand()} bng mt 480 nh rng, tr khi c cc nh ang ch bt u dch v, trong trng hp , 481 nhng nh ny s c chuyn. iu ny ph hp vi cc trnh pht phng tin (hoc dch v tng t) m khng 482 ang thc thi lnh, nhng ang chy v thi hn v ch mt tc v.</dd> 483 <dt>{@link android.app.Service#START_REDELIVER_INTENT}</dt> 484 <dd>Nu h thng tt b dch v sau khi {@link android.app.Service#onStartCommand 485 onStartCommand()} tr v, hy to li dch v v gi {@link 486 android.app.Service#onStartCommand onStartCommand()} bng nh cui cng c chuyn ti 487 dch v. Mi nh ch u c chuyn ln lt. iu ny ph hp vi cc dch v ang 488 ch ng thc hin mt cng vic m nn c tip tc ngay lp tc, chng hn nh ti xung mt tp.</dd> 489 </dl> 490 <p> bit thm chi tit v nhng gi tr tr v ny, hy xem ti liu tham kho c lin kt cho tng 491 hng s.</p> 492 493 494 495 <h3 id="StartingAService">Bt u mt Dch v</h3> 496 497 <p>Bn c th bt u mt dch v t mt hot ng hoc thnh phn ng dng khc bng cch chuyn mt 498 {@link android.content.Intent} (quy nh dch v s bt u) n {@link 499 android.content.Context#startService startService()}. H thng Android s gi phng php {@link 500 android.app.Service#onStartCommand onStartCommand()} ca dch v v chuyn cho n {@link 501 android.content.Intent}. (Bn tuyt i khng nn trc tip gi {@link android.app.Service#onStartCommand 502 onStartCommand()}.)</p> 503 504 <p>V d, mt hot ng c th bt u dch v v d trong phn trc ({@code 505 HelloSevice}) bng cch s dng mt nh biu th vi {@link android.content.Context#startService 506 startService()}:</p> 507 508 <pre> 509 Intent intent = new Intent(this, HelloService.class); 510 startService(intent); 511 </pre> 512 513 <p>Phng php {@link android.content.Context#startService startService()} ngay lp tc tr v v 514 h thng Android s gi phng php {@link android.app.Service#onStartCommand 515 onStartCommand()} ca dch v. Nu dch v khng ang chy, trc tin h thng s gi {@link 516 android.app.Service#onCreate onCreate()}, ri gi {@link android.app.Service#onStartCommand 517 onStartCommand()}.</p> 518 519 <p>Nu dch v cng khng cung cp kh nng gn kt, nh c chuyn bng {@link 520 android.content.Context#startService startService()} s l phng thc giao tip duy nht gia 521 thnh phn ng dng v dch v. Tuy nhin, nu bn mun dch v gi mt kt qu tr li, khi 522 my khch m bt u dch v c th to mt {@link android.app.PendingIntent} cho mt qung b 523 (bng {@link android.app.PendingIntent#getBroadcast getBroadcast()}) v chuyn n ti dch v 524 trong {@link android.content.Intent} m bt u dch v. Khi , dch v c th s dng 525 qung b chuyn kt qu.</p> 526 527 <p>Nhiu yu cu bt u dch v s dn n nhiu lnh gi tng ng ti 528 {@link android.app.Service#onStartCommand onStartCommand()} ca dch v. Tuy nhin, ch c mt yu cu dng 529 dch v (bng {@link android.app.Service#stopSelf stopSelf()} hoc {@link 530 android.content.Context#stopService stopService()}) l bt buc dng n.</p> 531 532 533 <h3 id="Stopping">Dng mt dch v</h3> 534 535 <p>Dch v c bt u phi qun l vng i ca chnh n. C th, h thng khng dng 536 hay hy dch v tr khi n phi khi phc b nh ca h thng v dch v 537 s tip tc chy sau khi {@link android.app.Service#onStartCommand onStartCommand()} tr v. V vy, 538 dch v phi t dng bng cch gi {@link android.app.Service#stopSelf stopSelf()} hoc mt thnh phn 539 khc c th dng n bng cch gi {@link android.content.Context#stopService stopService()}.</p> 540 541 <p>Sau khi c yu cu dng bng {@link android.app.Service#stopSelf stopSelf()} hoc {@link 542 android.content.Context#stopService stopService()}, h thng s hy dch v ngay khi 543 c th.</p> 544 545 <p>Tuy nhin, nu dch v ca bn x l nhiu yu cu {@link 546 android.app.Service#onStartCommand onStartCommand()} ng thi, khi bn khng nn dng 547 dch v khi bn hon thnh x l yu cu bt u, v bn c th nhn c mt 548 yu cu bt u mi k t thi im (dng khi kt thc yu cu th nht s chm dt yu cu th hai). trnh 549 vn ny, bn c th s dng {@link android.app.Service#stopSelf(int)} m bo rng yu cu 550 dng dch v ca bn lun c da trn yu cu bt u gn y nht. C th, khi bn gi {@link 551 android.app.Service#stopSelf(int)}, bn s chuyn ID ca yu cu bt u (<code>startId</code> 552 c chuyn ti {@link android.app.Service#onStartCommand onStartCommand()}) m yu cu dng ca bn 553 tng ng vi. Khi , nu dch v nhn c mt yu cu bt u mi trc khi bn c th gi {@link 554 android.app.Service#stopSelf(int)}, vy ID s khng khp v dch v s khng dng.</p> 555 556 <p class="caution"><strong>Ch :</strong> iu quan trng l ng dng ca bn dng dch v ca n 557 khi n hon thnh xong cng vic trnh lng ph ti nguyn ca h thng v tn pin. Nu cn, 558 cc thnh phn khc c th dng dch v bng cch gi {@link 559 android.content.Context#stopService stopService()}. Ngay c khi bn kch hot gn kt cho dch v, 560 bn phi lun t mnh dng dch v nu dch v nhn c lnh gi ti {@link 561 android.app.Service#onStartCommand onStartCommand()}.</p> 562 563 <p> bit thm thng tin v vng i ca mt dch v, hy xem phn bn di v <a href="#Lifecycle">Qun l Vng i ca mt Dch v</a>.</p> 564 565 566 567 <h2 id="CreatingBoundService">To mt Dch v Gn kt</h2> 568 569 <p>Dch v gn kt l mt dch v cho php cc thnh phn ng dng gn kt vi n bng cch gi {@link 570 android.content.Context#bindService bindService()} to mt kt ni lu di 571 (v thng khng cho php cc thnh phn <em>bt u</em> n bng cch gi {@link 572 android.content.Context#startService startService()}).</p> 573 574 <p>Bn nn to mt dch v gn kt khi mun tng tc vi dch v t hot ng 575 v cc thnh phn khc trong ng dng ca mnh hoc hin th mt s tnh nng trong ng dng ca bn cho 576 cc ng dng khc thng qua truyn thng lin tin trnh (IPC).</p> 577 578 <p> to mt dch v gn kt, bn phi trin khai phng php gi li {@link 579 android.app.Service#onBind onBind()} tr v mt {@link android.os.IBinder} m 580 nh ngha giao din cho giao tip vi dch v . Khi , cc thnh phn ng dng khc c th gi 581 {@link android.content.Context#bindService bindService()} truy xut giao din v 582 bt u cc phng php gi trn dch v. Dch v tn ti ch nhm phc v thnh phn ng dng m 583 c gn kt vi n, v th khi khng c thnh phn c gn kt vi dch v, h thng s hy n 584 (bn <em>khng</em> cn dng mt dch v gn kt theo cch phi lm khi dch v c bt u 585 thng qua {@link android.app.Service#onStartCommand onStartCommand()}).</p> 586 587 <p> to mt dch v gn kt, iu u tin bn phi lm l nh ngha giao din quy nh 588 cch thc m mt my khch c th giao tip vi dch v. Giao din gia dch v v 589 my khch ny phi l mt trin khai {@link android.os.IBinder} v c dch v ca bn phi 590 tr v t phng php gi li {@link android.app.Service#onBind 591 onBind()}. Sau khi my khch nhn c {@link android.os.IBinder}, n c th bt u 592 tng tc vi dch v thng qua giao din .</p> 593 594 <p>Nhiu my khch c th gn kt vi dch v ng thi. Khi mt my khch hon thnh tng tc vi 595 dch v, n s gi {@link android.content.Context#unbindService unbindService()} b gn kt. Sau khi 596 khng cn my khch no c gn kt vi dch v, h thng s hy dch v.</p> 597 598 <p>C nhiu cch trin khai mt dch v gn kt v trin khai s phc tp 599 hn so vi dch v c bt u, v th ni dung bn v dch v gn kt c trnh by trong mt 600 ti liu ring v <a href="{@docRoot}guide/components/bound-services.html">Dch v Gn kt</a>.</p> 601 602 603 604 <h2 id="Notifications">Gi Thng bo ti Ngi dng</h2> 605 606 <p>Sau khi chy, mt dch v c th thng bo cho ngi dng v s kin bng cch s dng <a href="{@docRoot}guide/topics/ui/notifiers/toasts.html">Thng bo Ca s</a> hoc <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Thng bo Thanh Trng thi</a>.</p> 607 608 <p>Thng bo ca s l mt thng bo xut hin mt lc trn b mt ca ca s hin ti 609 ri bin mt, trong khi thng bo thanh trng thi cung cp mt biu tng trong thanh trng thi cng mt 610 thng bo, ngi dng c th chn n thc hin mt hnh ng (chng hn nh bt u mt hot ng).</p> 611 612 <p>Thng thng thng bo thanh trng thi l k thut tt nht khi mt cng vic nn no hon thnh 613 (chng hn nh mt tp hon thnh 614 vic ti xung) v lc ny ngi dng c th hnh ng da trn n. Khi ngi dng chn thng bo t dng xem m rng 615 , thng bo c th bt u mt hot ng (chng hn nh xem tp c ti xung).</p> 616 617 <p>Xem hng dn dnh cho nh pht trin <a href="{@docRoot}guide/topics/ui/notifiers/toasts.html">Thng bo Ca s</a> hoc <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Thng bo Thanh Trng thi</a> 618 bit thm thng tin.</p> 619 620 621 622 <h2 id="Foreground">Chy mt Dch v trong Tin cnh</h2> 623 624 <p>Dch v tin cnh l mt dch v c coi l iu m 625 ngi dng ang ch ng quan tm, v th n khng c ngh h thng tt b khi b nh thp. Dch v 626 tin cnh phi cung cp mt thng bo cho thanh trng thi, n c t di tiu 627 "ang din ra", iu ny c ngha l thng bo khng th loi b c tr khi dch v 628 b dng hoc loi b khi tin cnh.</p> 629 630 <p>V d, mt trnh chi nhc ang pht nhc t mt dch v nn c t chy trong 631 tin cnh, v ngi dng r rng thc c 632 hot ng ca n. Thng bo trong thanh trng thi c th cho bit bi ht ang chi v cho php 633 ngi dng khi chy mt hot ng tng tc vi trnh chi nhc.</p> 634 635 <p> yu cu dch v ca bn chy trong tin cnh, hy gi {@link 636 android.app.Service#startForeground startForeground()}. Phng php ny dng hai tham s: mt s nguyn 637 xc nh duy nht thng bo v {@link 638 android.app.Notification} cho thanh trng thi. V d:</p> 639 640 <pre> 641 Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text), 642 System.currentTimeMillis()); 643 Intent notificationIntent = new Intent(this, ExampleActivity.class); 644 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 645 notification.setLatestEventInfo(this, getText(R.string.notification_title), 646 getText(R.string.notification_message), pendingIntent); 647 startForeground(ONGOING_NOTIFICATION_ID, notification); 648 </pre> 649 650 <p class="caution"><strong>Ch :</strong> ID s nguyn m bn cp cho {@link 651 android.app.Service#startForeground startForeground()} khng c bng 0.</p> 652 653 654 <p> xa b dch v khi tin cnh, hy gi {@link 655 android.app.Service#stopForeground stopForeground()}. Phng php ny dng mt boolean, cho bit 656 c loi b c thng bo thanh trng thi hay khng. Phng php ny <em>khng</em> dng 657 dch v. Tuy nhin, nu bn dng dch v trong khi n vn ang chy trong tin cnh, khi thng bo 658 cng b loi b.</p> 659 660 <p> bit thm thng tin v thng bo, hy xem phn <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">To Thng bo 661 Thanh Trng thi</a>.</p> 662 663 664 665 <h2 id="Lifecycle">Qun l Vng i ca mt Dch v</h2> 666 667 <p>Vng i ca mt dch v n gin hn nhiu so vi vng i ca mt hot ng. Tuy nhin, mt iu thm ch cn quan trng hn 668 l bn phi tht ch ti cch dch v ca bn c to v hy, bi mt dch v 669 c th chy ngm m ngi dng khng bit.</p> 670 671 <p>Vng i ca dch v—t khi n c to ti khi n b hy—c th i theo hai 672 con ng khc nhau:</p> 673 674 <ul> 675 <li>Dch v c bt u 676 <p>Dch v c to khi mt thnh phn khc gi {@link 677 android.content.Context#startService startService()}. Sau , dch v s chy v thi hn v phi 678 t dng bng cch gi {@link 679 android.app.Service#stopSelf() stopSelf()}. Mt thnh phn khc cng c th dng 680 dch v bng cch gi {@link android.content.Context#stopService 681 stopService()}. Khi dch v b dng, h thng s hy n.</p></li> 682 683 <li>Dch v gn kt 684 <p>Dch v c to khi mt thnh phn khc (my khch) gi {@link 685 android.content.Context#bindService bindService()}. Khi , my khch giao tip vi dch v 686 thng qua mt giao din {@link android.os.IBinder}. My khch c th ng kt ni bng cch gi 687 {@link android.content.Context#unbindService unbindService()}. Nhiu my khch c th gn kt vi 688 cng dch v v khi tt c chng b gn kt, h thng s hy dch v. (Dch v 689 <em>khng</em> cn t mnh dng.)</p></li> 690 </ul> 691 692 <p>Hai con ng ny hon ton ring bit. C th, bn c th gn kt vi mt dch v 693 c bt u bng {@link android.content.Context#startService startService()}. V d, mt dch v 694 nhc nn c th c bt u bng cch gi {@link android.content.Context#startService 695 startService()} bng mt {@link android.content.Intent} m s nhn bit nhc pht. Sau , 696 c th l khi ngi dng mun thc thi mt quyn iu khin i vi trnh pht hoc ly thng tin v 697 bi ht ang pht, hot ng c th gn kt vi dch v bng cch gi {@link 698 android.content.Context#bindService bindService()}. Trong nhng trng hp nh vy, {@link 699 android.content.Context#stopService stopService()} hoc {@link android.app.Service#stopSelf 700 stopSelf()} khng thc s dng dch v ti khi tt c my khch b gn kt. </p> 701 702 703 <h3 id="LifecycleCallbacks">Trin khai gi li vng i</h3> 704 705 <p>Ging nh mt hot ng, dch v c cc phng php gi li vng i m bn c th trin khai theo di 706 nhng thay i v trng thi ca dch v v thc hin cng vic ti nhng thi im ph hp. Dch v khung sau 707 minh ha tng phng php vng i:</p> 708 709 <pre> 710 public class ExampleService extends Service { 711 int mStartMode; // indicates how to behave if the service is killed 712 IBinder mBinder; // interface for clients that bind 713 boolean mAllowRebind; // indicates whether onRebind should be used 714 715 @Override 716 public void {@link android.app.Service#onCreate onCreate}() { 717 // The service is being created 718 } 719 @Override 720 public int {@link android.app.Service#onStartCommand onStartCommand}(Intent intent, int flags, int startId) { 721 // The service is starting, due to a call to {@link android.content.Context#startService startService()} 722 return <em>mStartMode</em>; 723 } 724 @Override 725 public IBinder {@link android.app.Service#onBind onBind}(Intent intent) { 726 // A client is binding to the service with {@link android.content.Context#bindService bindService()} 727 return <em>mBinder</em>; 728 } 729 @Override 730 public boolean {@link android.app.Service#onUnbind onUnbind}(Intent intent) { 731 // All clients have unbound with {@link android.content.Context#unbindService unbindService()} 732 return <em>mAllowRebind</em>; 733 } 734 @Override 735 public void {@link android.app.Service#onRebind onRebind}(Intent intent) { 736 // A client is binding to the service with {@link android.content.Context#bindService bindService()}, 737 // after onUnbind() has already been called 738 } 739 @Override 740 public void {@link android.app.Service#onDestroy onDestroy}() { 741 // The service is no longer used and is being destroyed 742 } 743 } 744 </pre> 745 746 <p class="note"><strong>Lu :</strong> Khng nh cc phng php gi li vng i ca hot ng, bn 747 <em>khng</em> phi gi trin khai siu lp vi nhng phng php gi li ny.</p> 748 749 <img src="{@docRoot}images/service_lifecycle.png" alt="" /> 750 <p class="img-caption"><strong>Hnh 2.</strong> Vng i dch v. S pha bn tri 751 minh ha vng i khi dch v c to bng {@link android.content.Context#startService 752 startService()} v s pha bn phi minh ha vng i khi dch v c to 753 bng {@link android.content.Context#bindService bindService()}.</p> 754 755 <p>Bng vic trin khai nhng phng php ny, bn c th theo di hai vng lp lng nhau trong vng i ca dch v: </p> 756 757 <ul> 758 <li><strong>Ton b vng i</strong> ca mt dch v xy ra gia thi im gi {@link 759 android.app.Service#onCreate onCreate()} v thi im {@link 760 android.app.Service#onDestroy} tr v. Ging nh hot ng, dch v thc hin thit lp ban u ca n trong 761 {@link android.app.Service#onCreate onCreate()} v gii phng tt c ti nguyn cn li trong {@link 762 android.app.Service#onDestroy onDestroy()}. V d, mt 763 dch v pht li nhc c th to lung m ti nhc s c pht trong {@link 764 android.app.Service#onCreate onCreate()}, sau dng lung trong {@link 765 android.app.Service#onDestroy onDestroy()}. 766 767 <p>Cc phng php {@link android.app.Service#onCreate onCreate()} v {@link android.app.Service#onDestroy 768 onDestroy()} c gi cho tt c dch v, d 769 chng c to bi {@link android.content.Context#startService startService()} hay {@link 770 android.content.Context#bindService bindService()}.</p></li> 771 772 <li><strong>Vng i hin hot</strong> ca mt dch v s bt u bng mt lnh gi n hoc {@link 773 android.app.Service#onStartCommand onStartCommand()} hoc {@link android.app.Service#onBind onBind()}. 774 Mi phng php s c giao {@link 775 android.content.Intent} m c chuyn tng ng cho hoc {@link android.content.Context#startService 776 startService()} hoc {@link android.content.Context#bindService bindService()}. 777 <p>Nu dch v c bt u, vng i hin hot s chm dt ti cng thi im khi ton b vng i 778 chm dt (dch v s vn hin hot ngay c sau khi {@link android.app.Service#onStartCommand 779 onStartCommand()} tr v). Nu dch v b gn kt, vng i hin hot s chm dt khi {@link 780 android.app.Service#onUnbind onUnbind()} tr v.</p> 781 </li> 782 </ul> 783 784 <p class="note"><strong>Lu :</strong> Mc d dch v c bt u b dng bi mt lnh gi n 785 hoc {@link android.app.Service#stopSelf stopSelf()} hoc {@link 786 android.content.Context#stopService stopService()}, s khng c mt lnh gi li tng ng cho 787 dch v (khng c lnh gi li {@code onStop()}). V th, tr khi dch v c gn kt vi mt my khch, 788 h thng s hy n khi dch v b dng—{@link 789 android.app.Service#onDestroy onDestroy()} l lnh gi li duy nht nhn c.</p> 790 791 <p>Hnh 2 minh ha cc phng php gi li in hnh cho mt dch v. Mc d hnh tch ring 792 cc dch v c to bi {@link android.content.Context#startService startService()} vi cc dch v 793 c to bi {@link android.content.Context#bindService bindService()}, hy 794 ghi nh rng bt k dch v no, d c bt u nh th no, u c th cho php my khch gn kt vi n. 795 V th, mt dch v c bt u t u bng {@link android.app.Service#onStartCommand 796 onStartCommand()} (bi mt my khch gi {@link android.content.Context#startService startService()}) 797 vn c th nhn mt lnh gi n {@link android.app.Service#onBind onBind()} (khi my khch gi 798 {@link android.content.Context#bindService bindService()}).</p> 799 800 <p> bit thm thng tin v vic tao mt dch v c tnh nng gn kt, hy xem ti liu <a href="{@docRoot}guide/components/bound-services.html">Dch v Gn kt</a>, 801 trong c thm thng tin v phng php gi li {@link android.app.Service#onRebind onRebind()} 802 trong phn v <a href="{@docRoot}guide/components/bound-services.html#Lifecycle">Qun l Vng i ca 803 mt Dch v Gn kt</a>.</p> 804 805 806 <!-- 807 <h2>Beginner's Path</h2> 808 809 <p>To learn how to query data from the system or other applications (such as contacts or media 810 stored on the device), continue with the <b><a 811 href="{@docRoot}guide/topics/providers/content-providers.html">Content Providers</a></b> 812 document.</p> 813 --> 814