1 page.title= 2 @jd:body 3 4 <div id="qv-wrapper"> 5 <ol id="qv"> 6 <h2></h2> 7 <ol> 8 <li><a href="#Basics"></a></li> 9 <ol> 10 <li><a href="#Declaring"></a></li> 11 </ol> 12 <li><a href="#CreatingAService"></a> 13 <ol> 14 <li><a href="#ExtendingIntentService"> IntentService </a></li> 15 <li><a href="#ExtendingService"></a></li> 16 <li><a href="#StartingAService"></a></li> 17 <li><a href="#Stopping"></a></li> 18 </ol> 19 </li> 20 <li><a href="#CreatingBoundService"></a></li> 21 <li><a href="#Notifications"></a></li> 22 <li><a href="#Foreground"></a></li> 23 <li><a href="#Lifecycle"></a> 24 <ol> 25 <li><a href="#LifecycleCallbacks"></a></li> 26 </ol> 27 </li> 28 </ol> 29 30 <h2></h2> 31 <ol> 32 <li>{@link android.app.Service}</li> 33 <li>{@link android.app.IntentService}</li> 34 </ol> 35 36 <h2></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></h2> 45 <ol> 46 <li><a href="{@docRoot}guide/components/bound-services.html"></a></li> 47 </ol> 48 49 </div> 50 51 52 <p>{@link android.app.Service} 53 54 55 (IPC) 56 / 57 58 </p> 59 60 <p></p> 61 62 <dl> 63 <dt></dt> 64 <dd> ( Activity) 65 {@link android.content.Context#startService startService()} 66 67 68 69 </dd> 70 <dt></dt> 71 <dd> {@link 72 android.content.Context#bindService bindService()} (IPC) 73 74 75 76 </dd> 77 </dl> 78 79 <p> — () 80 {@link 81 android.app.Service#onStartCommand onStartCommand()} {@link 82 android.app.Service#onBind onBind()} 83 </p> 84 85 <p> () Activity — {@link android.content.Intent} 86 87 88 <a href="#Declaring"></a> 89 90 </p> 91 92 <p class="caution"><strong></strong> — <strong></strong><strong></strong> () 93 94 CPU ( MP3 ) 95 96 (ANR) Activity 97 98 </p> 99 100 101 <h2 id="Basics"></h2> 102 103 <div class="sidebox-wrapper"> 104 <div class="sidebox"> 105 <h3></h3> 106 <p> 107 108 </p> 109 <p> 110 Activity 111 {@link android.app.Activity#onCreate onCreate()} {@link 112 android.app.Activity#onStart onStart()} {@link android.app.Activity#onStop 113 onStop()} 114 {@link android.os.AsyncTask} {@link android.os.HandlerThread} {@link java.lang.Thread} 115 <a href="{@docRoot}guide/components/processes-and-threads.html#Threads"></a> 116 </p> 117 <p> 118 119 </p> 120 </div> 121 </div> 122 123 <p> {@link android.app.Service} () 124 125 126 </p> 127 128 <dl> 129 <dt>{@link android.app.Service#onStartCommand onStartCommand()}</dt> 130 <dd> ( Activity) {@link android.content.Context#startService 131 startService()} 132 133 {@link android.app.Service#stopSelf stopSelf()} {@link 134 android.content.Context#stopService stopService()} 135 () 136 </dd> 137 <dt>{@link android.app.Service#onBind onBind()}</dt> 138 <dd> ( RPC) {@link android.content.Context#bindService 139 bindService()} 140 {@link android.os.IBinder} 141 null 142 </dd> 143 <dt>{@link android.app.Service#onCreate()}</dt> 144 <dd> ( {@link android.app.Service#onStartCommand onStartCommand()} 145 {@link android.app.Service#onBind onBind()} ) 146 147 </dd> 148 <dt>{@link android.app.Service#onDestroy()}</dt> 149 <dd> 150 151 </dd> 152 </dl> 153 154 <p> {@link 155 android.content.Context#startService startService()} ( {@link 156 android.app.Service#onStartCommand onStartCommand()} ) {@link android.app.Service#stopSelf()} 157 {@link android.content.Context#stopService stopService()} 158 </p> 159 160 <p> 161 {@link android.content.Context#bindService bindService()} ( {@link 162 android.app.Service#onStartCommand onStartCommand()}<em></em>) 163 164 </p> 165 166 <p> Activity Android 167 Activity<a href="#Foreground"></a> () 168 — 169 170 171 172 ( {@link 173 android.app.Service#onStartCommand onStartCommand()} ) 174 <a href="{@docRoot}guide/components/processes-and-threads.html"></a> 175 176 </p> 177 178 <p> 179 </p> 180 181 182 183 <h3 id="Declaring"></h3> 184 185 <p> Activity () 186 </p> 187 188 <p> <a href="{@docRoot}guide/topics/manifest/service-element.html">{@code <service>}</a> <a href="{@docRoot}guide/topics/manifest/application-element.html">{@code <application>}</a> 189 190 </p> 191 192 <pre> 193 <manifest ... > 194 ... 195 <application ... > 196 <service android:name=".ExampleService" /> 197 ... 198 </application> 199 </manifest> 200 </pre> 201 202 <p> <a href="{@docRoot}guide/topics/manifest/service-element.html">{@code <service>}</a> 203 </p> 204 205 <p> <a href="{@docRoot}guide/topics/manifest/service-element.html">{@code <service>}</a> 206 207 <a href="{@docRoot}guide/topics/manifest/service-element.html#nm">{@code android:name}</a> — 208 (<a href="http://android-developers.blogspot.com/2011/06/things-that-cannot-change.html"></a>) 209 210 211 212 213 <p><strong> {@link android.app.Service} </strong> 214 {@link 215 android.content.Intent} {@link 216 android.content.Intent#setPackage setPackage()} 217 218 219 </p> 220 221 <p> <a href="{@docRoot}guide/topics/manifest/service-element.html#exported">{@code android:exported}</a> {@code "false"} 222 223 224 </p> 225 226 227 228 229 <h2 id="CreatingStartedService"></h2> 230 231 <p> {@link 232 android.content.Context#startService startService()} 233 {@link android.app.Service#onStartCommand onStartCommand()} </p> 234 235 <p> 236 237 {@link android.app.Service#stopSelf stopSelf()} {@link android.content.Context#stopService stopService()} 238 239 </p> 240 241 <p> Activity {@link 242 android.content.Context#startService startService()} 243 {@link android.content.Intent} {@link android.app.Service#onStartCommand 244 onStartCommand()} {@link android.content.Intent} 245 </p> 246 247 <p> Activity {@link 248 android.content.Context#startService startService()}Activity 249 {@link 250 android.app.Service#onStartCommand onStartCommand()} 251 252 </p> 253 254 <p class="caution"><strong></strong> 255 Activity Activity 256 257 258 </p> 259 260 <p></p> 261 <dl> 262 <dt>{@link android.app.Service}</dt> 263 <dd> Activity 264 265 266 </dd> 267 <dt>{@link android.app.IntentService}</dt> 268 <dd> {@link android.app.Service} worker 269 270 {@link 271 android.app.IntentService#onHandleIntent onHandleIntent()} 272 </dd> 273 </dl> 274 275 <p> 276 </p> 277 278 279 <h3 id="ExtendingIntentService"> IntentService </h3> 280 281 <p>() {@link android.app.IntentService} 282 283 </p> 284 285 <p>{@link android.app.IntentService} </p> 286 287 <ul> 288 <li> worker {@link 289 android.app.Service#onStartCommand onStartCommand()} 290 </li> 291 <li> {@link 292 android.app.IntentService#onHandleIntent onHandleIntent()} 293 </li> 294 <li> 295 {@link android.app.Service#stopSelf}</li> 296 <li> null {@link android.app.IntentService#onBind onBind()} 297 </li> 298 <li> {@link android.app.IntentService#onStartCommand 299 onStartCommand()} {@link 300 android.app.IntentService#onHandleIntent onHandleIntent()} </li> 301 </ul> 302 303 <p> {@link 304 android.app.IntentService#onHandleIntent onHandleIntent()} 305 ()</p> 306 307 <p> {@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>{@link 343 android.app.IntentService#onHandleIntent onHandleIntent()} </p> 344 345 <p> {@link 346 android.app.IntentService#onCreate onCreate()}{@link 347 android.app.IntentService#onStartCommand onStartCommand()} {@link 348 android.app.IntentService#onDestroy onDestroy()} 349 {@link android.app.IntentService} worker </p> 350 351 <p>{@link android.app.IntentService#onStartCommand onStartCommand()} ( {@link 352 android.app.IntentService#onHandleIntent onHandleIntent()}) 353 </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> {@link android.app.IntentService#onHandleIntent onHandleIntent()} {@link android.app.IntentService#onBind 364 onBind()} () 365 </p> 366 367 <p> {@link android.app.Service} 368 369 </p> 370 371 372 <h3 id="ExtendingService"></h3> 373 374 <p> {@link android.app.IntentService} 375 () {@link android.app.Service} 376 377 </p> 378 379 <p> {@link 380 android.app.Service} {@link 381 android.app.IntentService} worker 382 </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> {@link android.app.IntentService}</p> 456 457 <p> {@link android.app.Service#onStartCommand 458 onStartCommand()} () 459 460 </p> 461 462 <p>{@link android.app.Service#onStartCommand onStartCommand()} 463 ( {@link 464 android.app.IntentService} ) 465 466 {@link android.app.Service#onStartCommand onStartCommand()} 467 </p> 468 469 <dl> 470 <dt>{@link android.app.Service#START_NOT_STICKY}</dt> 471 <dd> {@link android.app.Service#onStartCommand 472 onStartCommand()} <em></em> 473 474 </dd> 475 <dt>{@link android.app.Service#START_STICKY}</dt> 476 <dd> {@link android.app.Service#onStartCommand 477 onStartCommand()} {@link 478 android.app.Service#onStartCommand onStartCommand()}<em></em> 479 null {@link android.app.Service#onStartCommand onStartCommand()} 480 481 () 482 </dd> 483 <dt>{@link android.app.Service#START_REDELIVER_INTENT}</dt> 484 <dd> {@link android.app.Service#onStartCommand 485 onStartCommand()} {@link 486 android.app.Service#onStartCommand onStartCommand()} 487 488 </dd> 489 </dl> 490 <p> 491 </p> 492 493 494 495 <h3 id="StartingAService"></h3> 496 497 <p> 498 {@link android.content.Intent} () {@link 499 android.content.Context#startService startService()} Activity Android {@link 500 android.app.Service#onStartCommand onStartCommand()} {@link 501 android.content.Intent} ( {@link android.app.Service#onStartCommand 502 onStartCommand()})</p> 503 504 <p> {@link android.content.Context#startService 505 startService()}Activity ({@code 506 HelloSevice})</p> 507 508 <pre> 509 Intent intent = new Intent(this, HelloService.class); 510 startService(intent); 511 </pre> 512 513 <p>{@link android.content.Context#startService startService()} Android {@link android.app.Service#onStartCommand 514 onStartCommand()} 515 {@link 516 android.app.Service#onCreate onCreate()} {@link android.app.Service#onStartCommand 517 onStartCommand()}</p> 518 519 <p> {@link 520 android.content.Context#startService startService()} 521 {@link android.app.PendingIntent}( {@link android.app.PendingIntent#getBroadcast getBroadcast()}) {@link android.content.Intent} 522 523 524 525 </p> 526 527 <p> 528 {@link android.app.Service#onStartCommand onStartCommand()} ( {@link android.app.Service#stopSelf stopSelf()} {@link 529 android.content.Context#stopService stopService()}) 530 </p> 531 532 533 <h3 id="Stopping"></h3> 534 535 <p> {@link android.app.Service#onStartCommand onStartCommand()} 536 537 {@link android.app.Service#stopSelf stopSelf()} {@link android.content.Context#stopService stopService()} 538 539 </p> 540 541 <p> {@link android.app.Service#stopSelf stopSelf()} {@link 542 android.content.Context#stopService stopService()} 543 </p> 544 545 <p> {@link 546 android.app.Service#onStartCommand onStartCommand()} () 547 548 {@link android.app.Service#stopSelf(int)} 549 550 {@link 551 android.app.Service#stopSelf(int)} ID (<code>startId</code> {@link android.app.Service#onStartCommand onStartCommand()}) 552 553 {@link 554 android.app.Service#stopSelf(int)} ID </p> 555 556 <p class="caution"><strong></strong> 557 {@link 558 android.content.Context#stopService stopService()} 559 {@link 560 android.app.Service#onStartCommand onStartCommand()} 561 </p> 562 563 <p><a href="#Lifecycle"></a></p> 564 565 566 567 <h2 id="CreatingBoundService"></h2> 568 569 <p> {@link 570 android.content.Context#bindService bindService()} ( {@link 571 android.content.Context#startService startService()} <em></em> ) 572 </p> 573 574 <p> (IPC) Activity 575 576 </p> 577 578 <p> {@link 579 android.app.Service#onBind onBind()} 580 {@link android.os.IBinder} () 581 {@link android.content.Context#bindService bindService()} 582 583 ( 584 {@link android.app.Service#onStartCommand onStartCommand()} <em></em>) 585 </p> 586 587 <p> 588 {@link android.os.IBinder} {@link android.app.Service#onBind 589 onBind()} 590 591 {@link android.os.IBinder} 592 </p> 593 594 <p> {@link android.content.Context#unbindService unbindService()} 595 596 </p> 597 598 <p><a href="{@docRoot}guide/components/bound-services.html"></a> 599 600 </p> 601 602 603 604 <h2 id="Notifications"></h2> 605 606 <p><a href="{@docRoot}guide/topics/ui/notifiers/toasts.html"></a><a href="{@docRoot}guide/topics/ui/notifiers/notifications.html"></a></p> 607 608 <p> ( Activity) 609 610 </p> 611 612 <p>() 613 614 Activity () 615 </p> 616 617 <p><a href="{@docRoot}guide/topics/ui/notifiers/toasts.html"></a><a href="{@docRoot}guide/topics/ui/notifiers/notifications.html"></a> 618 </p> 619 620 621 622 <h2 id="Foreground"></h2> 623 624 <p> 625 626 627 628 </p> 629 630 <p> 631 632 Activity 633 </p> 634 635 <p> {@link 636 android.app.Service#startForeground startForeground()} {@link 637 android.app.Notification} 638 </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></strong> {@link 651 android.app.Service#startForeground startForeground()} ID 0</p> 652 653 654 <p> {@link 655 android.app.Service#stopForeground stopForeground()} 656 <em></em> 657 658 </p> 659 660 <p><a href="{@docRoot}guide/topics/ui/notifiers/notifications.html"></a> 661 </p> 662 663 664 665 <h2 id="Lifecycle"></h2> 666 667 <p> Activity 668 669 </p> 670 671 <p> — — 672 </p> 673 674 <ul> 675 <li> 676 <p> {@link 677 android.content.Context#startService startService()} {@link 678 android.app.Service#stopSelf() stopSelf()} 679 {@link android.content.Context#stopService 680 stopService()} 681 </p></li> 682 683 <li> 684 <p> () {@link 685 android.content.Context#bindService bindService()} 686 {@link android.os.IBinder} 687 {@link android.content.Context#unbindService unbindService()} 688 (<em></em>) 689 </p></li> 690 </ul> 691 692 <p> 693 {@link android.content.Context#startService startService()} {@link android.content.Context#startService 694 startService()} {@link android.content.Intent} 695 Activity {@link 696 android.content.Context#bindService bindService()} 697 698 {@link 699 android.content.Context#stopService stopService()} {@link android.app.Service#stopSelf 700 stopSelf()} </p> 701 702 703 <h3 id="LifecycleCallbacks"></h3> 704 705 <p> Activity 706 707 </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></strong> Activity <em></em> 747 </p> 748 749 <img src="{@docRoot}images/service_lifecycle.png" alt="" /> 750 <p class="img-caption"><strong> 2.</strong> {@link android.content.Context#startService 751 startService()} 752 {@link android.content.Context#bindService bindService()} 753 </p> 754 755 <p> </p> 756 757 <ul> 758 <li><strong></strong> {@link 759 android.app.Service#onCreate onCreate()} {@link 760 android.app.Service#onDestroy} Activity 761 {@link android.app.Service#onCreate onCreate()} {@link 762 android.app.Service#onDestroy onDestroy()} {@link 763 android.app.Service#onCreate onCreate()} {@link 764 android.app.Service#onDestroy onDestroy()} 765 766 767 <p>{@link android.app.Service#onCreate onCreate()} {@link android.app.Service#onDestroy 768 onDestroy()} {@link android.content.Context#startService startService()} {@link 769 android.content.Context#bindService bindService()} 770 </p></li> 771 772 <li><strong></strong> {@link 773 android.app.Service#onStartCommand onStartCommand()} {@link android.app.Service#onBind onBind()} 774 {@link 775 android.content.Intent} {@link android.content.Context#startService 776 startService()} {@link android.content.Context#bindService bindService()} 777 <p> ( {@link android.app.Service#onStartCommand 778 onStartCommand()} ) 779 {@link 780 android.app.Service#onUnbind onUnbind()} </p> 781 </li> 782 </ul> 783 784 <p class="note"><strong></strong> 785 {@link android.app.Service#stopSelf stopSelf()} {@link 786 android.content.Context#stopService stopService()} ( {@code onStop()} ) 787 — {@link 788 android.app.Service#onDestroy onDestroy()} 789 </p> 790 791 <p> 2 792 {@link android.content.Context#startService startService()} 793 {@link android.content.Context#bindService bindService()} 794 795 {@link android.app.Service#onStartCommand 796 onStartCommand()} ( {@link android.content.Context#startService startService()}) {@link android.app.Service#onBind onBind()} ( 797 {@link android.content.Context#bindService bindService()} ) 798 </p> 799 800 <p><a href="{@docRoot}guide/components/bound-services.html"></a><a href="{@docRoot}guide/components/bound-services.html#Lifecycle"></a> {@link android.app.Service#onRebind onRebind()} 801 802 803 </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