Home | History | Annotate | Download | only in components
      1 page.title=  
      2 page.tags= ,
      3 
      4 @jd:body
      5 
      6 <div id="qv-wrapper">
      7 <div id="qv">
      8 
      9 <h2> </h2>
     10 <ol>
     11 <li><a href="#Processes"></a>
     12   <ol>
     13     <li><a href="#Lifecycle">  </a></li>
     14   </ol>
     15 </li>
     16 <li><a href="#Threads"></a>
     17   <ol>
     18     <li><a href="#WorkerThreads"> </a></li>
     19     <li><a href="#ThreadSafe"> </a></li>
     20   </ol>
     21 </li>
     22 <li><a href="#IPC"> </a></li>
     23 </ol>
     24 
     25 </div>
     26 </div>
     27 
     28 <p>        
     29 ,  Android    Linux     
     30 .            
     31 (  ).       
     32    (      ),  
     33          .    
     34         
     35    .</p>
     36 
     37 <p>          Android.</p>
     38 
     39 
     40 <h2 id="Processes"></h2>
     41 
     42 <p>         ,   
     43     . ,   ,     
     44 ,      .</p>
     45 
     46 <p>       &mdash;<a href="{@docRoot}guide/topics/manifest/activity-element.html">{@code
     47 &lt;activity&gt;}</a>, <a href="{@docRoot}guide/topics/manifest/service-element.html">{@code
     48 &lt;service&gt;}</a>, <a href="{@docRoot}guide/topics/manifest/receiver-element.html">{@code
     49 &lt;receiver&gt;}</a>  <a href="{@docRoot}guide/topics/manifest/provider-element.html">{@code
     50 &lt;provider&gt;}</a> &mdash;  {@code android:process},  
     51 ,      .     ,    
     52   ,  ,        .     
     53 {@code android:process} ,       
     54 ,          Linux    
     55  .</p>
     56 
     57 <p> <a href="{@docRoot}guide/topics/manifest/application-element.html">{@code
     58 &lt;application&gt;}</a>    {@code android:process},  
     59   ,     .</p>
     60 
     61 <p>Android      ,        
     62 ,      .  
     63 ,    ,  .      
     64 ,     .</p>
     65 
     66 <p>    ,  Android    
     67  .  ,    ,  , 
     68    ,    ,   . , 
     69       ,    .   ,
     70         . </p>
     71 
     72 
     73 <h3 id="Lifecycle">  </h3>
     74 
     75 <p> Android       , 
     76        ,         .  
     77 ,   ,
     78   ,         
     79 ,   ,    .      
     80     ,        ..,   
     81    .</p>
     82 
     83 <p>     .     
     84      (   <em> </em> 
     85 <em>   </em>):</p>
     86 
     87 <ol>
     88   <li><b>  </b>
     89     <p>,     .  
     90     ,      :</p>
     91 
     92       <ul>
     93         <li>   {@link android.app.Activity},     (  {@link
     94 android.app.Activity}
     95 {@link android.app.Activity#onResume onResume()}).</li>
     96 
     97         <li>   {@link android.app.Service},   ,  
     98  .</li>
     99 
    100         <li>   {@link android.app.Service},   "  ",&mdash;,
    101   {@link android.app.Service#startForeground startForeground()}.
    102 
    103         <li>  {@link android.app.Service},      
    104    ({@link android.app.Service#onCreate onCreate()}, {@link android.app.Service#onStart
    105 onStart()}  {@link android.app.Service#onDestroy onDestroy()}).</li>
    106 
    107         <li>   {@link android.content.BroadcastReceiver},    {@link
    108 android.content.BroadcastReceiver#onReceive onReceive()}.</li>
    109     </ul>
    110 
    111     <p>       .    
    112   ,     ,       .     
    113       ,   ,       , 
    114      .</p></li>
    115 
    116   <li><b> </b>
    117     <p>,      ,  
    118     .   ,
    119      :</p>
    120 
    121       <ul>
    122         <li>   {@link android.app.Activity},      , 
    123   (  {@link android.app.Activity#onPause onPause()}). 
    124 ,   ,        ,   
    125    .</li>
    126 
    127         <li>   {@link android.app.Service},   
    128     .</li>
    129       </ul>
    130 
    131       <p>    ,   
    132   ,        . </p>
    133     </li>
    134 
    135   <li><b> </b>
    136     <p>,   ,     {@link
    137 android.content.Context#startService startService()},        
    138    .        ,   ,
    139        (,      
    140    ),     ,    
    141           . </p>
    142   </li>
    143 
    144   <li><b> </b>
    145     <p>,  ,        (  
    146 {@link android.app.Activity#onStop onStop()} ).     
    147    ,        ,    
    148   ,
    149    .     ,     
    150 LRU ( ),  ,  
    151  ,   ,    .        ,
    152     ,         
    153  ,        ,  
    154     .       .   <a href="{@docRoot}guide/components/activities.html#SavingActivityState"></a>
    155 .</p>
    156   </li>
    157 
    158   <li><b> </b>
    159     <p>,      .     
    160    ,     
    161    .           
    162        .</p>
    163   </li>
    164 </ol>
    165 
    166 
    167   <p> Android         
    168 ,      .  ,       ,
    169   ,    .</p>
    170 
    171   <p> ,     ,    ,   . 
    172 , ,   ,       
    173 . ,      A     B 
    174   A      B,  A    
    175 ,   B.</p>
    176 
    177   <p>  ,  ,      ,
    178 ,   ,   <a href="{@docRoot}guide/components/services.html"></a>   ,   
    179   ,   ,     .
    180 , ,     -,     
    181 ,              .
    182   ,         ,
    183   ,    .      
    184  ,       ,     .</p>
    185 
    186 
    187 
    188 
    189 <h2 id="Threads"></h2>
    190 
    191 <p>        ,
    192   .    ,       
    193     ,    .    ,  
    194          Android (   {@link
    195 android.widget}  {@link android.view}).  ,    ,   
    196   .</p>
    197 
    198 <p> <em></em>       . 
    199 ,     ,      ,   
    200      .  ,    
    201   (   {@link android.view.View#onKeyDown onKeyDown()}     
    202      ),       .</p>
    203 
    204 <p>,      ,      
    205    , ,   ,          
    206   .          ,   
    207  .</p>
    208 
    209 <p>         ,    
    210    ,    .  , 
    211      ,   ,     
    212    ,     .   ,     ,
    213    .    
    214   .  ,        
    215 (    5 ),      <a href="http://developer.android.com/guide/practices/responsiveness.html"> 
    216 </a>.         
    217   .</p>
    218 
    219 <p> ,     Andoid <em></em>  . ,    
    220      .       
    221   .  ,       Android:</p>
    222 
    223 <ol>
    224 <li>    
    225 <li>       Android    
    226 </ol>
    227 
    228 <h3 id="WorkerThreads"> </h3>
    229 
    230 <p>          
    231       .    ,
    232   ,       ( 
    233  ).</p>
    234 
    235 <p>,     ,     
    236       {@link android.widget.ImageView}:</p>
    237 
    238 <pre>
    239 public void onClick(View v) {
    240     new Thread(new Runnable() {
    241         public void run() {
    242             Bitmap b = loadImageFromNetwork("http://example.com/image.png");
    243             mImageView.setImageBitmap(b);
    244         }
    245     }).start();
    246 }
    247 </pre>
    248 
    249 <p>  ,    ,         
    250 . ,      : <em>      
    251 Android    </em>&mdash;    {@link
    252 android.widget.ImageView}   ,      .   
    253     ,    .</p>
    254 
    255 <p>    Android          
    256 .     :</p>
    257 
    258 <ul>
    259 <li>{@link android.app.Activity#runOnUiThread(java.lang.Runnable)
    260 Activity.runOnUiThread(Runnable)}</li>
    261 <li>{@link android.view.View#post(java.lang.Runnable) View.post(Runnable)}</li>
    262 <li>{@link android.view.View#postDelayed(java.lang.Runnable, long) View.postDelayed(Runnable,
    263 long)}</li>
    264 </ul>
    265 
    266 <p>,         {@link
    267 android.view.View#post(java.lang.Runnable) View.post(Runnable)}:</p>
    268 
    269 <pre>
    270 public void onClick(View v) {
    271     new Thread(new Runnable() {
    272         public void run() {
    273             final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png");
    274             mImageView.post(new Runnable() {
    275                 public void run() {
    276                     mImageView.setImageBitmap(bitmap);
    277                 }
    278             });
    279         }
    280     }).start();
    281 }
    282 </pre>
    283 
    284 <p>   :      ,
    285   {@link android.widget.ImageView}     .</p>
    286 
    287 <p>    ,        
    288  .        , 
    289   {@link android.os.Handler}      ,   
    290  . ,       {@link android.os.AsyncTask},
    291      ,      .</p>
    292 
    293 
    294 <h4 id="AsyncTask"> AsyncTask</h4>
    295 
    296 <p> {@link android.os.AsyncTask}      
    297 .             
    298        / .</p>
    299 
    300 <p>       {@link android.os.AsyncTask}      {@link
    301 android.os.AsyncTask#doInBackground doInBackground()},    
    302  .    ,    {@link
    303 android.os.AsyncTask#onPostExecute onPostExecute()},     {@link
    304 android.os.AsyncTask#doInBackground doInBackground()}      ,     
    305   .      {@link android.os.AsyncTask#execute execute()}
    306    .</p>
    307 
    308 <p>,        {@link android.os.AsyncTask} 
    309 :</p>
    310 
    311 <pre>
    312 public void onClick(View v) {
    313     new DownloadImageTask().execute("http://example.com/image.png");
    314 }
    315 
    316 private class DownloadImageTask extends AsyncTask&lt;String, Void, Bitmap&gt; {
    317     /** The system calls this to perform work in a worker thread and
    318       * delivers it the parameters given to AsyncTask.execute() */
    319     protected Bitmap doInBackground(String... urls) {
    320         return loadImageFromNetwork(urls[0]);
    321     }
    322     
    323     /** The system calls this to perform work in the UI thread and delivers
    324       * the result from doInBackground() */
    325     protected void onPostExecute(Bitmap result) {
    326         mImageView.setImageBitmap(result);
    327     }
    328 }
    329 </pre>
    330 
    331 <p>       ,     
    332 ,      ,  ,       .</p>
    333 
    334 <p>  {@link android.os.AsyncTask},   
    335   .      :</p>
    336 
    337 <ul>
    338 <li>   ,     
    339      </li>
    340 <li> {@link android.os.AsyncTask#doInBackground doInBackground()}  
    341   </li>
    342 <li> {@link android.os.AsyncTask#onPreExecute onPreExecute()}, {@link
    343 android.os.AsyncTask#onPostExecute onPostExecute()}  {@link
    344 android.os.AsyncTask#onProgressUpdate onProgressUpdate()}     </li>
    345 <li>,   {@link android.os.AsyncTask#doInBackground doInBackground()},   
    346 {@link android.os.AsyncTask#onPostExecute onPostExecute()}</li>
    347 <li>  {@link android.os.AsyncTask#publishProgress publishProgress()}     {@link
    348 android.os.AsyncTask#doInBackground doInBackground()}   {@link
    349 android.os.AsyncTask#onProgressUpdate onProgressUpdate()}    </li>
    350 <li>        </li>
    351 </ul>
    352 
    353 <p class="caution"><strong>!</strong>  ,        
    354 ,       <a href="{@docRoot}guide/topics/resources/runtime-changes.html">    </a>,
    355 (,     ),     . 
    356 ,               
    357   ,      <a href="http://code.google.com/p/shelves/">Shelves</a>.</p>
    358 
    359 
    360 <h3 id="ThreadSafe"> </h3>
    361 
    362 <p>           , ,
    363      . </p>
    364 
    365 <p>      ,    , ,    <a href="{@docRoot}guide/components/bound-services.html"> </a>.  
    366     {@link android.os.IBinder},     ,   
    367 {@link android.os.IBinder IBinder},      .
    368 ,      ,    ,   
    369 ,       ,   {@link android.os.IBinder
    370 IBinder} (       ).  ,  
    371 {@link android.app.Service#onBind onBind()}       
    372  , ,   ,   {@link android.app.Service#onBind
    373 onBind()} (, ,    RPC),    
    374  .       ,       
    375      {@link android.os.IBinder IBinder}.   {@link android.os.IBinder
    376 IBinder}      .</p>
    377 
    378 <p>        ,     .
    379   {@link android.content.ContentResolver}  {@link android.content.ContentProvider}
    380     ,  {@link
    381 android.content.ContentProvider},     ,&mdash; {@link
    382 android.content.ContentProvider#query query()}, {@link android.content.ContentProvider#insert
    383 insert()}, {@link android.content.ContentProvider#delete delete()}, {@link
    384 android.content.ContentProvider#update update()}  {@link android.content.ContentProvider#getType
    385 getType()}&mdash;       ,    
    386   .           ,
    387        . </p>
    388 
    389 
    390 <h2 id="IPC"> </h2>
    391 
    392 <p> Android     (IPC)     
    393 (RPC),         ,  
    394  (  )    
    395  .          , 
    396  ,           
    397  ,       .     
    398    .   Android        IPC,
    399            RPC. </p>
    400 
    401 <p>  IPC          {@link
    402 android.content.Context#bindService bindService()}.      <a href="{@docRoot}guide/components/services.html"></a>   .</p>
    403 
    404 
    405 <!--
    406 <h2>Beginner's Path</h2>
    407 
    408 <p>For information about how to perform work in the background for an indefinite period of time
    409 (without a user interface), continue with the <b><a
    410 href="{@docRoot}guide/components/services.html">Services</a></b> document.</p>
    411 -->
    412