1. 程式人生 > >Android中的Service詳解

Android中的Service詳解

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

今天我們就來介紹一下Android中的四大元件中的服務Service,說到Service,

它分為本地服務和遠端服務:區分這兩種服務就是看客戶端和服務端是否在同一個程序中,本地服務是在同一程序中的,遠端服務是不在同一個程序中的。

開啟服務也有兩種方式,一種是startService(),他對應的結束服務的方法是stopService(),另一種是bindService(),結束服務的是unBindService(),這兩種方式的區別就是:當客戶端Client使用startService方法開啟服務的時候,這個服務和Client之間就沒有聯絡了,Service的執行和Client是相互獨立的,想結束這個服務的話,就在服務本身中呼叫stopSelf()方法結束服務。而當客戶端Client使用bindService方法開始服務的時候,這個服務和Client是一種關聯的關係,他們之間使用Binder的代理物件進行互動,這個在後面會詳細說到,要是結束服務的話,需要在Client中和服務斷開,呼叫unBindService方法。

在這裡我們只做bindService方式的研究,而startService方式比較獨立和簡單,這裡就不做演示了。


首先來說一下本地服務:

本地服務很簡單的,就是Client和這個服務在同一個程序中:

先來看一下程式碼吧:

下面這張圖是專案的結構圖:


為了方便資料的訪問,這裡定義一個數據的訪問介面:

[java]  view plain copy 在CODE上檢視程式碼片 派生到我的程式碼片
  1. package com.nativeservice.demo;  
  2.   
  3. /** 
  4.  * 訪問介面 
  5.  * @author weijiang204321 
  6.  */  
  7. public interface IStudent {  
  8.     /** 
  9.      * 通過no訪問name 
  10.      * @param no 
  11.      * @return 
  12.      */  
  13.     public String getNameByNumber(int no);  
  14.       
  15. }  

下面再來看一下StudentService的程式碼:

[java]  view plain copy 在CODE上檢視程式碼片 派生到我的程式碼片
  1. package com.nativeservice.demo;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.os.Binder;  
  6. import android.os.IBinder;  
  7.   
  8. /** 
  9.  * 定義的服務Service 
  10.  * @author weijiang204321 
  11.  */  
  12. public class StudentService extends Service{  
  13.   
  14.     //名稱  
  15.     public static String[] nameAry = {"張飛","李小龍","趙薇"};  
  16.       
  17.     /** 
  18.      * 通過no獲取name 
  19.      * @param no 
  20.      * @return 
  21.      */  
  22.     private String getNameByNo(int no){  
  23.         if(no>0 && no<4)  
  24.             return nameAry[no-1];  
  25.         return null;  
  26.     }  
  27.       
  28.     @Override  
  29.     public IBinder onBind(Intent arg0) {  
  30.         return new StudentBinder();  
  31.     }  
  32.       
  33.     /** 
  34.      * 自定義的Binder物件 
  35.      * @author weijiang204321 
  36.      * 
  37.      */  
  38.     private class StudentBinder extends Binder implements IStudent{  
  39.         @Override  
  40.         public String getNameByNumber(int no) {  
  41.             return getNameByNo(no);  
  42.         }  
  43.     }  
  44.   
  45.   
  46. }  


StudentService中就是定義一個訪問name的方法,在onBind方法中返回Binder物件,這個就是Client和Service之間互動的關鍵物件

下面看一下Client程式碼:

[java]  view plain copy 在CODE上檢視程式碼片 派生到我的程式碼片
  1. package com.nativeservice.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.ComponentName;  
  5. import android.content.Intent;  
  6. import android.content.ServiceConnection;  
  7. import android.os.Bundle;  
  8. import android.os.IBinder;  
  9. import android.os.Looper;  
  10. import android.widget.Toast;  
  11.   
  12. /** 
  13.  * 測試Service 
  14.  * @author weijiang204321 
  15.  * 
  16.  */  
  17. public class MainActivity extends Activity {  
  18.   
  19.     private IStudent student;  
  20.       
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.activity_main);  
  25.         //開啟查詢名稱的服務  
  26.         Intent service = new Intent(this,StudentService.class);  
  27.         bindService(service,new StudentConnection(),BIND_AUTO_CREATE);  
  28.         //延遲2s在顯示查詢的內容,不然開啟服務也是需要時間的,如果不延遲一段時間的話,student物件為null;  
  29.         new Thread(){  
  30.             @Override  
  31.             public void run(){  
  32.                 try {  
  33.                     Thread.sleep(2*1000);  
  34.                     Looper.prepare();  
  35.                     Toast.makeText(getApplicationContext(), student.getNameByNumber(1), Toast.LENGTH_LONG).show();  
  36.                     Looper.loop();  
  37.                 } catch (InterruptedException e) {  
  38.                     e.printStackTrace();  
  39.                 }  
  40.             }  
  41.         }.start();  
  42.     }  
  43.       
  44.     /** 
  45.      * 自定義的服務連線connection 
  46.      * @author weijiang204321 
  47.      * 
  48.      */  
  49.     private class StudentConnection implements ServiceConnection{  
  50.           
  51.         @Override  
  52.         public void onServiceConnected(ComponentName name, IBinder service) {  
  53.             student = (IStudent)service;  
  54.         }  
  55.         @Override  
  56.         public void onServiceDisconnected(ComponentName name) {  
  57.         }  
  58.           
  59.     }  
  60. }  


在這裡,用到了bindService方法,該方法的引數是:第一個引數是服務的intent,第二引數是一個ServiceConnection介面,第三個引數是啟動服務的方式常量,這裡最主要的就是第二個引數ServiceConnection介面,我們自己定義一個實現該介面的類。

StudentConnection,必須實現兩個方法,這兩個方法見名思議,一個是連線時呼叫的方法,一個是斷開連線時的方法,在開始連線的方法onServiceConnected中傳回來一個IBinder物件,這個時候需要將其轉化一下,這個就是為什麼要在開始的時候定義一個IStudent介面,在這裡訪問資料就方便了,同時在Client程式碼中要做個延遲的操作來訪問資料,因為開啟服務,連線這個過程是需要時間的,所以在這裡就延遲了2s,這裡只是為了能夠正常顯示資料,才這麼做的,不然student物件是為null的,當然要根據自己的實際情況操作。最後還要在AndroidMainfest.xml中配置Service:

[html]  view plain copy