Home | History | Annotate | Download | only in components
      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">  Service</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    .  ,      
     56         (IPC). ,  
     57   ,  ,  -      ,  
     58    .</p>
     59 
     60 <p>     :</p>
     61 
     62 <dl>
     63   <dt></dt>
     64   <dd>  ,    (, )  
     65  {@link android.content.Context#startService startService()}.   
     66         ,    ,   . 
     67           .
     68 ,        .   ,
     69    .</dd>
     70   <dt></dt>
     71   <dd>  ,        {@link
     72 android.content.Context#bindService bindService()}.     -,
     73      ,  ,    
     74         (IPC).     
     75      .        ,
     76      ,  .</dd>
     77 </dl>
     78 
     79 <p>         ,  
     80        (     )   .
     81        : {@link
     82 android.app.Service#onStartCommand onStartCommand()}    ,  {@link
     83 android.app.Service#onBind onBind()}   .</p>
     84 
     85 <p>    (,     )   
     86    (   )  ,     
     87      {@link android.content.Intent}.     
     88            .  
     89     <a href="#Declaring"> 
     90  </a>.</p>
     91 
     92 <p class="caution"><strong>!</strong>  
     93       <strong></strong>   
     94  <strong></strong>     (    ).  ,
     95                (,  MP3
     96   ),           . 
     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  ,  , ,    ,   . ,
    111      ,      ,   
    112   {@link android.app.Activity#onCreate onCreate()},      {@link
    113 android.app.Activity#onStart onStart()},       {@link android.app.Activity#onStop
    114 onStop()}.      {@link android.os.AsyncTask}  {@link android.os.HandlerThread}
    115    {@link java.lang.Thread}.   <a href="{@docRoot}guide/components/processes-and-threads.html#Threads">
    116  </a>      .</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>   ,   , , ,
    131    ,  {@link android.content.Context#startService
    132 startService()}.            
    133    .     ,    
    134   {@link android.app.Service#stopSelf stopSelf()}  {@link
    135 android.content.Context#stopService stopService()}. (    ,
    136     ).</dd>
    137   <dt>{@link android.app.Service#onBind onBind()}</dt>
    138     <dd>   ,      
    139   (,     )   {@link android.content.Context#bindService
    140 bindService()}.         ,  
    141     ,  {@link android.os.IBinder}.   
    142  ,       ,    null.</dd>
    143   <dt>{@link android.app.Service#onCreate()}</dt>
    144     <dd>          
    145   (  {@link android.app.Service#onStartCommand onStartCommand()} 
    146 {@link android.app.Service#onBind onBind()}).    ,   
    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()}),  
    157  ,        {@link android.app.Service#stopSelf()}  
    158       {@link android.content.Context#stopService stopService()}.</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> Android        ,    ,    
    167  ,     .     ,     ,
    168  ,    ,      <a href="#Foreground">   </a> (  ),      .
    169   ,       ,         
    170  ,      
    171     ,       
    172 .     ,   ,    
    173    (     ,   {@link
    174 android.app.Service#onStartCommand onStartCommand()},   ).  
    175  ,          <a href="{@docRoot}guide/components/processes-and-threads.html">  </a>
    176 .</p>
    177 
    178 <p>          
    179     .</p>
    180 
    181 
    182 
    183 <h3 id="Declaring">   </h3>
    184 
    185 <p> ,    (  ),     
    186   .</p>
    187 
    188 <p>  ,   <a href="{@docRoot}guide/topics/manifest/service-element.html">{@code &lt;service&gt;}</a>
    189 ,    <a href="{@docRoot}guide/topics/manifest/application-element.html">{@code &lt;application&gt;}</a>
    190 . :</p>
    191 
    192 <pre>
    193 &lt;manifest ... &gt;
    194   ...
    195   &lt;application ... &gt;
    196       &lt;service android:name=".ExampleService" /&gt;
    197       ...
    198   &lt;/application&gt;
    199 &lt;/manifest&gt;
    200 </pre>
    201 
    202 <p>    
    203   .     <a href="{@docRoot}guide/topics/manifest/service-element.html">{@code &lt;service&gt;}</a>.</p>
    204 
    205 <p>  ,      <a href="{@docRoot}guide/topics/manifest/service-element.html">{@code &lt;service&gt;}</a> 
    206  , ,    ,  ,
    207     .  <a href="{@docRoot}guide/topics/manifest/service-element.html#nm">{@code android:name}</a>
    208          . 
    209         ,    
    210  -    , ,      (   <a href="http://android-developers.blogspot.com/2011/06/things-that-cannot-change.html">, 
    211  </a>   ).
    212 
    213 <p>    <strong>     
    214   {@link android.app.Service}</strong>       .  
    215       ,   ,  
    216            {@link
    217 android.content.Intent},           {@link
    218 android.content.Intent#setPackage setPackage()},     
    219   .</p>
    220 
    221 <p>         ,
    222   <a href="{@docRoot}guide/topics/manifest/service-element.html#exported">{@code android:exported}</a>
    223      {@code "false"}.      
    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    ,   .         
    238    {@link android.app.Service#stopSelf stopSelf()},      
    239   {@link android.content.Context#stopService stopService()}.</p>
    240 
    241 <p> , , ,   ,   {@link
    242 android.content.Context#startService startService()}    {@link android.content.Intent},
    243      ,    .  
    244   {@link android.content.Intent}   {@link android.app.Service#onStartCommand
    245 onStartCommand()}.</p>
    246 
    247 <p>,          .  
    248        ,     {@link
    249 android.content.Context#startService startService()}.      {@link
    250 android.app.Service#onStartCommand onStartCommand()},      
    251   .   ,  
    252   .</p>
    253 
    254 <p class="caution"><strong>!</strong>        ,   ,
    255    ,       . ,   
    256     ,           
    257 ,     .       
    258 ,       .</p>
    259 
    260 <p>   ,        :</p>
    261 <dl>
    262   <dt>{@link android.app.Service}</dt>
    263   <dd>     .     , 
    264   ,       ,        
    265 ,     , 
    266   .</dd>
    267   <dt>{@link android.app.IntentService}</dt>
    268   <dd>   {@link android.app.Service},       
    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>          ,
    282 (     ),   ,  
    283       {@link android.app.IntentService}.</p>
    284 
    285 <p> {@link android.app.IntentService}  :</p>
    286 
    287 <ul>
    288   <li>    ,    ,    {@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>   {@link android.app.IntentService#onBind onBind()}  , 
    297  null.</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   &#64;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() &lt; 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}       .</p>
    350 
    351 <p>,  {@link android.app.IntentService#onStartCommand onStartCommand()}  
    352    (    {@link
    353 android.app.IntentService#onHandleIntent onHandleIntent()}):</p>
    354 
    355 <pre>
    356 &#64;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()},  ,
    364       ,   {@link android.app.IntentService#onBind
    365 onBind()} (      ,     ).</p>
    366 
    367 <p>    ,        
    368   {@link android.app.Service},     ,   
    369 ,       .</p>
    370 
    371 
    372 <h3 id="ExtendingService">  Service</h3>
    373 
    374 <p>     ,   {@link android.app.IntentService}  
    375   . ,  ,   
    376   (      ), 
    377   {@link android.app.Service}    .</p>
    378 
    379 <p>       {@link
    380 android.app.Service},     ,    ,   {@link
    381 android.app.IntentService}.            
    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       &#64;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() &lt; 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   &#64;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   &#64;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   &#64;Override
    443   public IBinder onBind(Intent intent) {
    444       // We don't provide binding, so return null
    445       return null;
    446   }
    447 
    448   &#64;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   .    ,        ,
    464     (  ,      {@link
    465 android.app.IntentService}   ,     ). ,
    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      {@link android.app.Service#onStartCommand onStartCommand()}  ,
    480    null,       .    ,
    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()}.  Android   {@link
    500 android.app.Service#onStartCommand onStartCommand()}     {@link
    501 android.content.Intent}. (        {@link android.app.Service#onStartCommand
    502 onStartCommand()} ).</p>
    503 
    504 <p>,          ({@code
    505 HelloSevice}),      {@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> {@link android.content.Context#startService startService()}  ,  
    514 Android    {@link android.app.Service#onStartCommand
    515 onStartCommand()}.     ,    {@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    . ,   ,     ,
    522 ,   ,    {@link android.app.PendingIntent}  
    523 (  {@link android.app.PendingIntent#getBroadcast getBroadcast()})     
    524   {@link android.content.Intent},   .    
    525    .</p>
    526 
    527 <p>         
    528 {@link android.app.Service#onStartCommand onStartCommand()} .          
    529  (  {@link android.app.Service#stopSelf stopSelf()}  {@link
    530 android.content.Context#stopService stopService()}).</p>
    531 
    532 
    533 <h3 id="Stopping"> </h3>
    534 
    535 <p>      .  ,     
    536  ,      ,  
    537       {@link android.app.Service#onStartCommand onStartCommand()}. 
    538        {@link android.app.Service#stopSelf stopSelf()},  
    539        {@link android.content.Context#stopService stopService()}.</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   (         ).  
    549  ,     {@link android.app.Service#stopSelf(int)}, ,    
    550         .  ,    {@link
    551 android.app.Service#stopSelf(int)},      ( <code>startId</code>,
    552   {@link android.app.Service#onStartCommand onStartCommand()}),   
    553  . ,        ,     {@link
    554 android.app.Service#stopSelf(int)},         .</p>
    555 
    556 <p class="caution"><strong>!</strong>          ,
    557         .  
    558         {@link
    559 android.content.Context#stopService stopService()}.       ,
    560     ,   -   {@link
    561 android.app.Service#onStartCommand onStartCommand()}.</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()}    
    571 (     <em></em>    {@link
    572 android.content.Context#startService startService()}).</p>
    573 
    574 <p>    ,        
    575           
    576      (IPC).</p>
    577 
    578 <p>   ,      {@link
    579 android.app.Service#onBind onBind()}    {@link android.os.IBinder},
    580      .       
    581  {@link android.content.Context#bindService bindService()}    
    582    .          ,
    583 ,   ,   ,   
    584 ( <em></em>    ,     , 
    585  {@link android.app.Service#onStartCommand onStartCommand()}).</p>
    586 
    587 <p>   ,      , 
    588   .    
    589       {@link android.os.IBinder},    
    590      {@link android.app.Service#onBind
    591 onBind()}.  ,     {@link android.os.IBinder},   
    592      .</p>
    593 
    594 <p>       .    
    595  ,   {@link android.content.Context#unbindService unbindService()}   .  
    596     ,   ,   .</p>
    597 
    598 <p>     ,    ,
    599    ,       
    600  <a href="{@docRoot}guide/components/bound-services.html"> </a>.</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>   ,      ,
    609             ,
    610    ,    (   ).</p>
    611 
    612 <p>        ,   -  
    613 (, 
    614  ),    .     
    615  ,     (,    ).</p>
    616 
    617 <p>  .     <a href="{@docRoot}guide/topics/ui/notifiers/toasts.html"> </a> <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">
    618    </a>.</p>
    619 
    620 
    621 
    622 <h2 id="Foreground">    </h2>
    623 
    624 <p>    ,    
    625 ,             . 
    626        ,    
    627 .  ,      ,  
    628        .</p>
    629 
    630 <p>,  ,     ,     
    631   ,      
    632  .           
    633       .</p>
    634 
    635 <p>           {@link
    636 android.app.Service#startForeground startForeground()}.     :  ,
    637       {@link
    638 android.app.Notification}   . :</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>   ID,      {@link
    651 android.app.Service#startForeground startForeground()},     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"> 
    661   </a>.</p>
    662 
    663 
    664 
    665 <h2 id="Lifecycle">   </h2>
    666 
    667 <p>    ,    . ,  
    668    ,      ,   
    669        .</p>
    670 
    671 <p>          
    672  :</p>
    673 
    674 <ul>
    675 <li> 
    676   <p> ,      {@link
    677 android.content.Context#startService startService()}.         
    678      {@link
    679 android.app.Service#stopSelf() stopSelf()}.      
    680    {@link android.content.Context#stopService
    681 stopService()}.   ,   .</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  ,      ,   . (
    689 <em></em>   .)</p></li>
    690 </ul>
    691 
    692 <p>        .      ,   
    693    {@link android.content.Context#startService startService()}. , 
    694         {@link android.content.Context#startService
    695 startService()}   {@link android.content.Intent},     . ,
    696 ,             ,
    697          {@link
    698 android.content.Context#bindService bindService()}.     {@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> ,       ,     
    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     &#64;Override
    716     public void {@link android.app.Service#onCreate onCreate}() {
    717         // The service is being created
    718     }
    719     &#64;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     &#64;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     &#64;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     &#64;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     &#64;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>         , 
    747 <em></em>        .</p>
    748 
    749 <img src="{@docRoot}images/service_lifecycle.png" alt="" />
    750 <p class="img-caption"><strong> 2.</strong>   .   
    751   ,      {@link android.content.Context#startService
    752 startService()},       ,   
    753   {@link android.content.Context#bindService bindService()}.</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}.  ,      
    761 {@link android.app.Service#onCreate onCreate()}        {@link
    762 android.app.Service#onDestroy onDestroy()}.  ,
    763            {@link
    764 android.app.Service#onCreate onCreate()},      {@link
    765 android.app.Service#onDestroy onDestroy()}.
    766 
    767 <p> {@link android.app.Service#onCreate onCreate()}  {@link android.app.Service#onDestroy
    768 onDestroy()}    ,    :
    769 {@link android.content.Context#startService startService()}  {@link
    770 android.content.Context#bindService bindService()}.</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>  ,       
    778    (       {@link android.app.Service#onStartCommand
    779 onStartCommand()}).    ,    ,    {@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()},       
    787 (   {@code onStop()}). ,      ,
    788        {@link
    789 android.app.Service#onDestroy onDestroy()}      .</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()}),
    797     {@link android.app.Service#onBind onBind()} (  
    798  {@link android.content.Context#bindService bindService()}).</p>
    799 
    800 <p>    ,   , .   <a href="{@docRoot}guide/components/bound-services.html"> </a>,
    801         {@link android.app.Service#onRebind onRebind()}
    802   <a href="{@docRoot}guide/components/bound-services.html#Lifecycle">  
    803  </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