Android重要組件之一 Service 服務講解學習(一)

來源:互聯網
上載者:User

 

在Android有非同步處理需要重要的Service和Handler組件,今天首先學習一下Service組件,

     其中涉及到Android Service中處理序間通訊和bindService()的方法下一次在講解學習

  1:首先來看看在文檔中對於Service的概念的解釋:

    Service是Android系統中的一種組件,重要性可以堪比Activity,但是也有顯著的區別,Activity可以和使用者進行互動,但是Service只能運行在後台,不能和使用者直接互動;

    當我們退出應用的時候,Service還會在後台運行,進程不會結束;特別注意我們需要使用的Service的時候,我們需要在AndroidManifset.xml檔案的service標籤進行註冊

    然後我們我們有兩種啟動Service的方法包括(①:Context.startService() 和②:Context.bindService()

   2:需要使用Service的地方:

    在文檔中舉了下面兩個例子

     最讓我們想到的是應該是播放器的時候,我們可能需要邊聽歌邊幹些其他的事情,此時我們會退出播放器的應用,如果不適用Service的話,一退出應該應用我們就不能聽歌了;還有比如我們需要通過網路擷取資料,由於使用網路擷取資料的速度比較慢,此時我們可以使用Service來在後台進行擷取更新,隔一段時間把擷取到的資料發回,而不是讓我每次要擷取資料都要開啟應用;

 3:Service與Activity通訊:

Service後端的資料最終還是要呈現在前端Activity之上的,

在啟動Service的時候,文檔中這樣描述:Note that services, like other application objects, run in the main thread of their hosting process

 主要該Service,和其他應用對象一樣,運行其應用進程主線程上

   4:Service 啟動方式:

一、context.startService()

二、context.bindService();

使用Service之前必須在AndroidMainfest.xml 中使用<service android:name=".service的類名"/>進行註冊

 5:Service的使用方法(我今天主要學習的是startService)

   第一步:自己寫一個繼承Service類的類

   第二步:在AndroidMainfest.xml檔案中進行註冊

   第三步:startService()

 

 

 

  6:生命週期:

  首先看下聲明周期的運行圖

 

 

  

 

 ①:context.startService() 啟動Service是會經曆一下幾個方法

啟動的時候:context.startService() ---> onCreate() ---> onStartCommand()

  銷毀的時候:ontext.stopService()   ---> onDestroy();

 如果Service沒運行,則此時會先調用onCreate()方法,然後再調用onStartCommand();

 如果Service已經在運行,則只調用onStartCommand(),Service的onStartCommand(),方法可能會調用多次

 ②:

 啟動時候:context.bindService() ---> onCreate() --->onBind();

 銷毀的時候:onUnibind() ---> onDestroy()

onBind() 將會給用戶端返回一個IBind介面的執行個體,此時用戶端可以去調用服務的方法,不過此時Activity與Service算是綁定在一起了

意思是說如果Activity退出,那麼此時的Service也會退出

 

 

接下來是執行個體Demo:

 

繼承Service的方法: 

 

import android.app.Service; 

import android.content.Intent; 

import android.os.IBinder; 

 

public class ServiveTest extends Service { 

 

    @Override 

    public IBinder onBind(Intent intent) { 

        // TODO Auto-generated method stub 

        return null; 

    } 

 

    @Override 

    public void onCreate() { 

        System.out.println("onCreat------->"); 

        super.onCreate(); 

    } 

 

    @Override 

    public void onDestroy() { 

        // TODO Auto-generated method stub 

        System.out.println("onDestroy------->"); 

        super.onDestroy(); 

    } 

 

    @Override 

    public int onStartCommand(Intent intent, int flags, int startId) { 

        System.out.println("onStartCommand------->"); 

        return super.onStartCommand(intent, flags, startId); 

    } 

 

 

   

 

今天就初步學習了一下Service的使用context.startService()的方法,下一次會進行使用context.bindService()和處理序間通訊的學習

摘自 寒夜---菜鳥學習之路

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.