乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      service

       老匹夫 2014-01-23

      service運(yùn)行在主進(jìn)程的主線程中

      service在manifest中的配置。如果你想讓service只在本應(yīng)用程序中有效(即不被別的應(yīng)用程序訪問到)有兩種方法

         1:不要配置<intenfilter>,沒有<intentfilter>,別的應(yīng)用程序訪問不到你的service,你要訪問啟動(dòng)service只能顯示啟動(dòng)(intent中明確指出要啟動(dòng)的service的類名)

         2:在<service>標(biāo)簽中配置android:exported="false"屬性(不論你有沒有配置<intentfilter>此service都將是應(yīng)用程序私有的)。

      if your service performs intensive or blocking operations while the user interacts with an activity from the same application, 

      the service will slow down activity performance. To avoid impacting application performance,you should start a new thread inside the service.

      A started service must manage its own lifecycle. That is, the system does not stop or destroy 

      the service unless it must recover system memory and the service continues to run after onStartCommand() returns. 

      So, the service must stop itself by calling stopSelf() or another component can stop it by calling stopService().

      It's important that your application stops its services when it's done working, to avoid wasting system resources and consuming battery power.

      service一旦運(yùn)行,將在后臺(tái)一直運(yùn)行,直到你顯示停止service。service一般不會(huì)被android回收(內(nèi)存非常緊張的時(shí)候還是會(huì)被回收的),

              一般activity會(huì)優(yōu)先被回收,實(shí)在沒辦法了,才會(huì)考慮回收service。

              在service里開啟一個(gè)線程去執(zhí)行下載(或者其他的耗時(shí)的任務(wù))而不在activity里直接開啟一個(gè)線程去下載是因?yàn)?,一旦activity所在的進(jìn)程被殺死,他的子線程也會(huì)被殺死。

              而service的生存能力比較強(qiáng),只要服務(wù)不停止(即沒顯示停止service),則服務(wù)所在的進(jìn)程里就有活動(dòng)的組件(四大組件,activity,broadcast,service,contentprovider),

              這樣該進(jìn)程就不是空進(jìn)程(沒有任何活動(dòng)組件的進(jìn)程,空進(jìn)程容易被殺死,那么正在工作的子線程也會(huì)被殺死)則該進(jìn)程就不會(huì)那么容易被android回收。所有service里所開啟的線程一般不會(huì)被殺死。

       

      1、Service的種類

       

      按運(yùn)行地點(diǎn)分類:

       

      類別 區(qū)別 優(yōu)點(diǎn) 缺點(diǎn) 應(yīng)用

       

      本地服務(wù)(Local) 該服務(wù)依附在主進(jìn)程上, 服務(wù)依附在主進(jìn)程上而不是獨(dú)立的進(jìn)程,這樣在一定程度上節(jié)約了資源,另外Local服務(wù)因?yàn)槭窃谕贿M(jìn)程因此不需要IPC,也不需要AIDL。相應(yīng)bindService會(huì)方便很多。 主進(jìn)程被Kill后,服務(wù)便會(huì)終止。 非常常見的應(yīng)用如:HTC的音樂播放服務(wù),天天動(dòng)聽音樂播放服務(wù)。

       

      遠(yuǎn)程服務(wù)(Remote) 該服務(wù)是獨(dú)立的進(jìn)程, 服務(wù)為獨(dú)立的進(jìn)程,對(duì)應(yīng)進(jìn)程名格式為所在包名加上你指定的android:process字符串。由于是獨(dú)立的進(jìn)程,因此在Activity所在進(jìn)程被Kill的時(shí)候,該服務(wù)依然在運(yùn)行,不受其他進(jìn)程影響,有利于為多個(gè)進(jìn)程提供服務(wù)具有較高的靈活性。 該服務(wù)是獨(dú)立的進(jìn)程,會(huì)占用一定資源,并且使用AIDL進(jìn)行IPC稍微麻煩一點(diǎn)。 一些提供系統(tǒng)服務(wù)的Service,這種Service是常駐的。

       

      其實(shí)remote服務(wù)還是很少見的,并且一般都是系統(tǒng)服務(wù)。

       

      按運(yùn)行類型分類:

       

      類別 區(qū)別 應(yīng)用

       

      前臺(tái)服務(wù) 會(huì)在通知一欄顯示 ONGOING 的 Notification, 當(dāng)服務(wù)被終止的時(shí)候,通知一欄的 Notification 也會(huì)消失,這樣對(duì)于用戶有一定的通知作用。常見的如音樂播放服務(wù)。

       

      后臺(tái)服務(wù) 默認(rèn)的服務(wù)即為后臺(tái)服務(wù),即不會(huì)在通知一欄顯示 ONGOING 的 Notification。 當(dāng)服務(wù)被終止的時(shí)候,用戶是看不到效果的。某些不需要運(yùn)行或終止提示的服務(wù),如天氣更新,日期同步,郵件同步等。

       

      有同學(xué)可能會(huì)問,后臺(tái)服務(wù)我們可以自己創(chuàng)建 ONGOING 的 Notification 這樣就成為前臺(tái)服務(wù)嗎?答案是否定的,前臺(tái)服務(wù)是在做了上述工作之后需要調(diào)用 startForeground ( android 2.0 及其以后版本 )或 setForeground (android 2.0 以前的版本)使服務(wù)成為 前臺(tái)服務(wù)。這樣做的好處在于,當(dāng)服務(wù)被外部強(qiáng)制終止掉的時(shí)候,ONGOING 的 Notification 任然會(huì)移除掉。

       

      BoundService:

      You should create a bound service when you want to interact with the service from activities and other components in your application or to expose some of your application's functionality to other applications, through interprocess communication (IPC)

      Multiple clients can connect to the service at once. However, the system calls your service's onBind() method to retrieve theIBinder only when the first client binds. The system then delivers the same IBinder to any additional clients that bind, without calling onBind() again.

       

      • A started service

        The service is created when another component calls startService(). The service then runs indefinitely and must stop itself by calling stopSelf(). Another component can also stop the service by callingstopService(). When the service is stopped, the system destroys it..

      • A bound service

        The service is created when another component (a client) calls bindService(). The client then communicates with the service through an IBinder interface. The client can close the connection by callingunbindService(). Multiple clients can bind to the same service and when all of them unbind, the system destroys the service. (The service does not need to stop itself.)

      These two paths are not entirely separate. That is, you can bind to a service that was already started withstartService(). For example, a background music service could be started by calling startService() with an Intent that identifies the music to play. Later, possibly when the user wants to exercise some control over the player or get information about the current song, an activity can bind to the service by callingbindService(). In cases like this, stopService() or stopSelf() does not actually stop the service until all clients unbind.

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多