<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                > 編寫:[kesenhoo](https://github.com/kesenhoo) - 原文:[http://developer.android.com/training/run-background-service/create-service.html](http://developer.android.com/training/run-background-service/create-service.html) IntentService為在單個后臺線程執行一個操作提供了一種直接的實現方式。它可以處理一個長時間操作的任務并確保不影響到UI的響應性。而且IntentService的執行并不受UI的生命周期的影響。所以繼續運行的情況下將關閉一個AsyncTask。 IntentService有下面幾個局限性: - 不可以直接和UI做交互。為了把他執行的結果體現在UI上,需要發送給[Activity](# "An activity represents a single screen with a user interface.")。 - 工作任務隊列是順序執行的,如果一個任務正在IntentService中執行,此時你再發送一個任務請求,這個任務會一直等待直到前面一個任務執行完畢。 - 正在執行的任務無法打斷。 然而,在大多數情況下,IntentService都是簡單后臺任務操作的理想選擇。 這節課會演示如何創建繼承的IntentService。同樣也會演示如何創建必須實現的回調[onHandleIntent()](http://developer.android.com/reference/android/app/IntentService.html#onHandleIntent(android.content.Intent) )。最后,還會解釋如何在manifest文件中定義這個IntentService。 ### 1)創建IntentService 為你的app創建一個IntentService組件,需要定義一個類,并繼承IntentService類,在里面重寫onHandleIntent()方法,如下所示: ~~~ public class RSSPullService extends IntentService { @Override protected void onHandleIntent(Intent workIntent) { // 從傳入的intent獲取數據 String dataString = workIntent.getDataString(); ... // 根據dataString的內容在這里進行操作 ... } } ~~~ 注意一個普通Service組件的其他回調,例如`onStartCommand()`會被IntentService自動調用。在IntentService中,要避免重寫那些回調。 ### 2)在Manifest文件中定義IntentService IntentService需要在manifest文件添加相應的條目,將此條目`<service>`作為`<application>`元素的子元素下進行定義,如下所示: ~~~ <application android:icon="@drawable/icon" android:label="@string/app_name"> ... <!-- 因為android:exported 被設置為false,該服務只能在本應用中使用 --> <service android:name=".RSSPullService" android:exported="false"/> ... <application/> ~~~ `android:name`屬性指明了IntentService的名字。 注意`<service>`標簽并沒有包含任何intent filter。因為發送任務給IntentService的[Activity](# "An activity represents a single screen with a user interface.")需要使用顯式Intent,所以不需要filter。這也意味著只有在同一個app或者其他使用同一個UserID的組件才能夠訪問到這個Service。 至此,你已經有了一個基本的IntentService類,你可以通過Intent對象向它發送操作請求。構造這些對象以及發送它們到你的IntentService的方式,將在接下來的課程中描述。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看