欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中使用IntentService創(chuàng)建后臺服務(wù)實例

 更新時間:2014年06月26日 08:50:58   作者:冰凍魚  
這篇文章主要介紹了Android中使用IntentService創(chuàng)建后臺服務(wù)實例,IntentService提供了在單個后臺線程運行操作的簡單結(jié)構(gòu),需要的朋友可以參考下

IntentService提供了在單個后臺線程運行操作的簡單結(jié)構(gòu)。這允許它操作耗時操作,而不影響UI響應(yīng)。同樣,IntentService也不影響UI生命周期事件,所以,它在某些可能關(guān)閉AsyncTask的情況下,仍會繼續(xù)運行(實測在Activity的onDestory里寫AsyncTask無法運行)。

IntentService有如下限制:

1.它不能直接影響UI。要把結(jié)果反映給UI,需要發(fā)給Activity
2.工作請求會順序運行。如果一個操作未結(jié)束,后面發(fā)送的操作必須等它結(jié)束(單線程)
3.IntentService里運行的操作無法被中斷

然而,在大多數(shù)情況下,IntentService是簡單后臺任務(wù)的首選方式。

本節(jié)展示了如何創(chuàng)建IntentService的子類,如何創(chuàng)建onHandleIntent()回調(diào),如何在AndroidManifest.xml聲明IntentService。

創(chuàng)建IntentService

定義一個IntentService的子類,覆蓋onHandleIntent()方法:

復(fù)制代碼 代碼如下:

public class RSSPullService extends IntentService {
    @Override
    protected void onHandleIntent(Intent workIntent) {
        // Gets data from the incoming Intent
        String dataString = workIntent.getDataString();
        ...
        // Do work here, based on the contents of dataString
        ...
    }
}

提示:其他Service正常的回調(diào),像 onStartCommand()在IntentService里會自動調(diào)用。在IntentService里,應(yīng)該避免覆蓋這些回調(diào)。

在AndroidManifest.xml里定義IntentService

IntentService也是Service),需要在AndroidManifest.xml里注冊。

復(fù)制代碼 代碼如下:

<application
        android:icon="@drawable/icon"
        android:label="@string/app_name">
        ...
        <!--
            Because android:exported is set to "false",
            the service is only available to this app.
        -->
        <service
            android:name=".RSSPullService"
            android:exported="false"/>
        ...
    <application/>

android:name屬性指定了IntentService的類名。

注意:&ltservice&gt節(jié)點不能包含intent filter。發(fā)送工作請求的Activity使用明確的Intent,會指定哪個IntentService。這也意味著,只有同一個app里的組件,或者另一個有相同user id的應(yīng)用才能訪問IntentService。

現(xiàn)在你有了基礎(chǔ)的IntentService類,可以用Intent對象發(fā)送工作請求。

創(chuàng)建發(fā)送工作請求傳給IntentService

創(chuàng)建一個明確的Intent,添加需要的數(shù)據(jù),調(diào)用startService()發(fā)送給IntentService

復(fù)制代碼 代碼如下:
/*
 * Creates a new Intent to start the RSSPullService
 * IntentService. Passes a URI in the
 * Intent's "data" field.
 */
mServiceIntent = new Intent(getActivity(), RSSPullService.class);
mServiceIntent.setData(Uri.parse(dataUrl));
//Call startService()
// Starts the IntentService
getActivity().startService(mServiceIntent);

提示:可以在Activity or Fragment的任意位置發(fā)送工作請求。如果你需要先取到用戶輸入,你可以在點擊事件或類似手勢的回調(diào)方法里發(fā)送工作請求。

一旦調(diào)用了startService(),IntentService會在onHandleIntent()工作,完了結(jié)束自身。

下一步是報告結(jié)果給原來的Activity或Fragment,下節(jié)講如何用BroadcastReceiver實現(xiàn)。請參考此文:http://www.dbjr.com.cn/article/51548.htm

相關(guān)文章

  • Android RecyclerView添加FootView和HeadView

    Android RecyclerView添加FootView和HeadView

    這篇文章主要介紹了Android RecyclerView添加FootView和HeadView的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Android自定義UI手勢密碼改進版源碼下載

    Android自定義UI手勢密碼改進版源碼下載

    這篇文章主要介紹了Android自定義UI手勢密碼改進版,為大家提供了手勢密碼源碼下載,,具有一定的實用性,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Android studio實現(xiàn)簡單的計算器

    Android studio實現(xiàn)簡單的計算器

    這篇文章主要為大家詳細(xì)介紹了Android studio實現(xiàn)簡單的計算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • 關(guān)于AndroidStudio新建與編譯項目速度慢解決辦法

    關(guān)于AndroidStudio新建與編譯項目速度慢解決辦法

    這篇文章主要介紹了關(guān)于AndroidStudio新建與編譯項目速度慢的解決辦法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • Android動態(tài)修改應(yīng)用圖標(biāo)與名稱的方法實例

    Android動態(tài)修改應(yīng)用圖標(biāo)與名稱的方法實例

    這篇文章主要給大家介紹了關(guān)于Android動態(tài)修改應(yīng)用圖標(biāo)與名稱的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • Android實現(xiàn)點擊獲取驗證碼60秒后重新獲取功能

    Android實現(xiàn)點擊獲取驗證碼60秒后重新獲取功能

    這篇文章主要為大家詳細(xì)介紹了Android點擊獲取驗證碼60秒后重新獲取驗證碼的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Android雙重SurfaceView實現(xiàn)彈幕效果

    Android雙重SurfaceView實現(xiàn)彈幕效果

    這篇文章主要為大家詳細(xì)介紹了Android雙重SurfaceView實現(xiàn)彈幕效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Flutter Drawer抽屜菜單示例詳解

    Flutter Drawer抽屜菜單示例詳解

    這篇文章主要為大家詳細(xì)介紹了Flutter Drawer抽屜菜單示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Android實現(xiàn)截屏并保存操作功能

    Android實現(xiàn)截屏并保存操作功能

    這篇文章主要介紹了Android實現(xiàn)截屏操作功能,即Android中截取當(dāng)前屏幕的功能,感興趣的小伙伴們可以參考一下
    2015-12-12
  • Android獲取系統(tǒng)儲存以及內(nèi)存信息的方法(二)

    Android獲取系統(tǒng)儲存以及內(nèi)存信息的方法(二)

    這篇文章主要為大家詳細(xì)介紹了Android獲取系統(tǒng)儲存以及內(nèi)存信息的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10

最新評論