Android中 service組件詳解_Android

來源:互聯網
上載者:User

 service組件跟activity組件及其類似,可以說service是沒有介面的activity,

當然service的生命週期和activity還是有一定的差別的。

   service組件一般用在什麼地方的,上面講了service組件沒有介面,不用跟使用者直接互動,

所以service組件一般運行在後台。比如做一些不需要介面的資料處理等等。

開發service需要兩個步驟:

   1,定義一個基礎service的子類。
    2,在AndroidManifest.xml 檔案中配置該service。

怎麼啟動service呢,想想啟動activity是不是有兩種方法:

    startActivity(intent),
    startActivityForResult(intent)

那麼啟動service也有兩種方法:

    startService(intent),
    bindService(Intent service,ServiceConnection conn,int flags),

兩者有什麼區別可以先看下面的代碼:

public class BindService extends Service{  private int count;  private boolean quit;  // 定義onBinder方法所返回的對象  private MyBinder binder = new MyBinder();  // 通過繼承Binder來實現IBinder類  public class MyBinder extends Binder  {    public int getCount()    {      // 擷取Service的運行狀態:count      return count;    }  }  // 必須實現的方法  @Override  public IBinder onBind(Intent intent)  {    System.out.println("Service is Binded");    // 返回IBinder對象    return binder;  }  // Service被建立時回調該方法。  @Override  public void onCreate()  {    super.onCreate();    System.out.println("Service is Created");    // 啟動一條線程、動態地修改count狀態值    new Thread()    {      @Override      public void run()      {        while (!quit)        {          try          {            Thread.sleep(1000);          }          catch (InterruptedException e)          {          }          count++;        }      }    }.start();      }  // Service被中斷連線時回調該方法  @Override  public boolean onUnbind(Intent intent)  {    System.out.println("Service is Unbinded");    return true;  }  // Service被關閉之前回調。  @Override  public void onDestroy()  {    super.onDestroy();    this.quit = true;    System.out.println("Service is Destroyed");  }    @Override  public void onRebind(Intent intent)   {    super.onRebind(intent);    this.quit = true;    System.out.println("Service is ReBinded");  }  }

上面的Service的作用是 簡單的開啟一個線程,每 1秒鐘 count++,這個count資料
通過 binder對象 傳遞給 訪問者。

待會再做詳解,先看下面的代碼怎麼啟動Service,並得到 Service的 count資料

public class MainActivity extends Activity{  Button startService_bnt , bindService_bnt;  // 保持所啟動的Service的IBinder對象  BindService.MyBinder binder;   @Override  public void onCreate(Bundle savedInstanceState)  {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);     startService_bnt = (Button) findViewById(R.id.start_bnt);    bindService_bnt = (Button) findViewById(R.id.bind_bnt);     //建立啟動Service的Intent     Intent intent = new Intent(this,BindService.class);      startService_bnt.setOnClickListener(new OnClickListener()    {      @Override      public void onClick(View source)      {        //綁定指定Serivce        startService(intent);      }    });          bindService_bnt.setOnClickListener(new OnClickListener()    {      @Override      public void onClick(View source)      {        //綁定指定Serivce        bindService(intent , conn , Service.BIND_AUTO_CREATE);                  Toast.makeText(MainActivity.this          , "Serivce的count值為:" + binder.getCount()          , 4000)          .show();      }    });    }      // 定義一個ServiceConnection對象  private ServiceConnection conn = new ServiceConnection()  {    // 當該Activity與Service串連成功時回調該方法    @Override    public void onServiceConnected(ComponentName name      , IBinder service)    {      System.out.println("--Service Connected--");      // 擷取Service的onBind方法所返回的MyBinder對象      binder = (BindService.MyBinder) service;    }    // 當該Activity與Service中斷連線時回調該方法    @Override    public void onServiceDisconnected(ComponentName name)    {      System.out.println("--Service Disconnected--");          }  };}

上面activity定義了兩個按鈕,點擊兩個按鈕有兩種不同的方法啟動Service:

 startService(intent), bindService(Intent service,ServiceConnection conn,int flags), 

現在來講解一下兩種啟動方式的區別,並解釋上面的代碼。

startService(intent)啟動Service呢它不具有與訪問者互動的能力,就像activity 的 startActivity(),它不能從新啟動的activity拿到返回資料一樣

而bindService(Intent service,ServiceConnection conn,int flags),就不一樣了

訪問者能從啟動的Service 拿到資料,怎麼拿到的呢,bindService的第二個參數 conn,該參數是一個 ServiceConnection  對象,當訪問者與Service串連成功 就會回調ServiceConnection  的 onServiceConnected() 方法 ,上面的程式就是在這個回調方法裡面拿到 IBinder  對象的。

可以在看一下

// 定義一個ServiceConnection對象  private ServiceConnection conn = new ServiceConnection()  {    // 當該Activity與Service串連成功時回調該方法    @Override    public void onServiceConnected(ComponentName name      , IBinder service)    {      System.out.println("--Service Connected--");      // 擷取Service的onBind方法所返回的MyBinder對象      binder = (BindService.MyBinder) service;    }    // 當該Activity與Service中斷連線時回調該方法    @Override    public void onServiceDisconnected(ComponentName name)    {      System.out.println("--Service Disconnected--");          }  };

簡單點也就是說 訪問者通過 bindService 綁定到 Service,綁定成功後會回調ServiceConnection 中的 onServiceConnected()方法,這個方法裡面有IBinder service 參數,這個參數就是 Service暴露給 訪問者的對象,訪問者拿到這個對象就可以訪問 Service的資料了

這就是 訪問者與Service資料互動的原理,是通過 IBinder 對象來傳遞的。

可能到這這裡你還對 binder = (BindService.MyBinder) service;這句代碼不理解。

 你肯能覺得 拿到的IBinder 對象不應該是上面Service代碼中onBind 方法返回的 binder 才是嘛,怎麼 強轉成 BindService.MyBinder 對象了。

而且返回的 binder  也沒 count資料,訪問者怎麼就能 binder.getCount() 得到資料呢。

@Override  public IBinder onBind(Intent intent)  {    System.out.println("Service is Binded");    // 返回IBinder對象    return binder;  }

別忘了 上面Service代碼裡面還對 IBinder 對象進行處理

// 通過繼承Binder來實現IBinder類  public class MyBinder extends Binder  {    public int getCount()    {      // 擷取Service的運行狀態:count      return count;    }  }

Binder 是 IBinder 的 實作類別,MyBinder 繼承Binder 並在裡面定義了個方法。

那麼 拿到  IBinder  對象 就相當於 拿到  MyBinder 對象,就可以訪問 getCount方法了,這也是 為什麼 binder = (BindService.MyBinder) service; 進行強轉,並且binder.getCount() 可以拿到 count 資料,因為 IBinder 裡面並沒有業務實現,是MyBinder 幫它實現了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.