Android筆記三十四.Service綜合執行個體二,android.service
綜合執行個體2:用戶端訪問遠程Service服務實現:通過一個按鈕來擷取遠程Service的狀態,並顯示在兩個文字框中。思路:假設A應用需要與B應用進行通訊,調用B應用中的getName()、getAuthor()方法,B應用以Service方式向A應用提供服務。所以,我們可以將A應用看成是用戶端,B應用為服務端,分別命名為AILDClient、AILDServer.
一、服務端應用程式1.src/com.example.aildserver/song.aidl:AILD檔案 當完成aidl檔案建立後,選擇儲存,eclipse會自動在項目的gen目錄中同步產生Song.java介面檔案。介面檔案中產生一個Stub抽象類別,裡麵包括aidl定義的方法,還包括一些其它輔助性的方法,如geName()、getSong()方法,我們可以通過這兩個方法實現用戶端讀寫Service服務端資料。
- package com.example.aildserver;
- interface Song
- {
- String getName();
- String getSong();
- }
位置如下:
編寫Aidl檔案時,需要注意: 1.介面名和aidl檔案名稱相同; 2.介面和方法前不用加存取權限修飾符public,private等,也不能用final,static; 3.Aidl預設支援的類型包話java基本類型(int、long、boolean等)和(String、List、Map、CharSequence),使用這些類型時不需要import聲明。對於List和Map中的元素類型必須是Aidl支援的類型。如果使用自訂類型作為參數或返回值,自訂類型必須實現Parcelable介面。 4.自訂類型和AIDL產生的其它介面類型在aidl描述檔案中,應該顯式import,即便在該類和定義的包在同一個包中。 5.在aidl檔案中所有非Java基本型別參數必須加上in、out、inout標記,以指明參數是輸入參數、輸出參數還是輸入輸出參數。 6.Java原始類型預設的標記為in,不能為其它標記。
2.src/com.example.aildserver/MyService.java功能:Service子類,完成Service服務開發核心步驟: (1)重寫Service的onBind()方法(用於返回一個IBinder對象)、onCreate()方法、onDestroy() 方法、onUnbind()方法; (2)定義一個Stub的子類,該內部類實現了IBinder、Song兩個介面,該子類對象將作為遠程Service的onBind()方法返回IBinder對象的代理傳給用戶端的ServiceConnection的onServiceConnected方法的第二個參數。
- package com.example.aildserver;
- import com.example.aildserver.Song.Stub;
- import android.app.Service;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.IBinder;
- import android.os.RemoteException;
- public class MyService extends Service {
- private String[] names = new String[] {"林俊傑","蔡依林","鄧紫棋"};
- private String[] songs = new String[] {"可惜沒如果","第三人稱","多遠都要在一起"};
- private String name,song;
- private int current=1; //當前位置
- private MyBinder binder = new MyBinder(); //執行個體化一個IBinder對象
- /*0.Stub內部類
- * 該內部類實現了IBinder、Song兩個介面,這個Stub類將會作為遠程Service的回調類。*/
- public class MyBinder extends Stub
- {
- //a.用戶端回調該方法擷取歌手名
- public String getName() throws RemoteException
- {
- return name;
- }
- //b.用戶端回調該方法擷取歌曲
- public String getSong() throws RemoteException
- {
- return song;
- }
-
- }
- /*1.onBind方法
- * service用於返回一個IBinder對象給用戶端方便通訊
- */
- @Override
- public IBinder onBind(Intent arg0) {
- return binder;
- }
- /*2.onCreate方法
- * 當Service啟動後,自動調用該方法,用於初始化
- * */
- public void onCreate() {
- name = names[current]; //給name、song賦值
- song = songs[current];
- System.out.println("Service print:name="+name+"song="+song);
- super.onCreate();
- }
- /*3.onDestroy方法
- * 當訪問者調用Context.stopService方法後,調用該方法關閉Service服務
- * */
- public void onDestroy() {
- super.onDestroy();
- }
- /*4.onUnbind方法
- * 當訪問者調調用Context.unBind()方法後,調用該方法與Service解除綁定*/
- public boolean onUnbind(Intent intent) {
- return false;
- }
-
- }
注意1:用戶端訪問Service時,Android並不是直接返回Service對象給用戶端,Service只是將一個回調對象(IBinder對象)通過onBind()方法回調給用戶端。 注意2:與綁定本地Service不同的是,本地Service的onBind()方法會直接把IBinder對象本身傳給用戶端的ServiceConnection的onServiceConnected方法的第二個參數。但遠程Service的onBind()方法只是將IBinder對象的代理傳給用戶端的ServiceConnection的onServiceConnected方法的第二個參數。當用戶端擷取了遠端Service的IBinder對象的代理之後,接下來可通過該IBinder對象去回調遠程Service的屬性或方法。
3.AndroidManifest.xml功能:配置Service組件,並指定其action屬性(方便其他應用程式啟動該Service服務)
- <application
- ........
- <!-- 配置service -->
- <service android:name=".MyService">
- <intent-filter>
- <action android:name="com.jiangdongguo.service"/>
- </intent-filter>
- </service>
- </application>
二、用戶端應用程式
1.拷貝服務端.aidl檔案到用戶端 把AIDLService應用中aidl檔案所在package連同aidl檔案一起拷貝到用戶端AIDLClient應用,eclipse會自動在A應用的gen目錄中為aidl檔案同步產生Song.java介面檔案,接下來就可以在AIDLClient應用中實現與AIDLService應用通訊。2.src/com.example.aildclient/MainActivity.java功能:(1)啟動服務端Service服務;(2)擷取返回的IBinder代理對象,並完成與服務端程式的通訊
- package com.example.aildclient;
-
- import com.example.aildserver.Song;
-
- import android.app.Activity;
- import android.app.Service;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.os.RemoteException;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class MainActivity extends Activity {
- private Button getBtn;
- private EditText song;
- private EditText name;
- private Song binder;
- //1.建立一個ServiceConnection對象
- private ServiceConnection conn = new ServiceConnection()
- {
- public void onServiceConnected(ComponentName name, IBinder service)
- {
- binder = Song.Stub.asInterface(service); //擷取Service返回的代理IBinder對象
-
- }
- public void onServiceDisconnected(ComponentName name) {
- }
- };
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- getBtn=(Button)findViewById(R.id.get);
- song=(EditText)findViewById(R.id.song);
- name=(EditText)findViewById(R.id.name);
- //2.指定要啟動的Service
- Intent intent = new Intent("com.jiangdongguo.service");
- bindService(intent, conn, Service.BIND_AUTO_CREATE);
- getBtn.setOnClickListener(new OnClickListener(){
-
- public void onClick(View arg0)
- {
- try {
- name.setText(binder.getName());
- song.setText(binder.getSong());
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- }
- });
- }
- }
對於遠程服務調用,遠程服務返回給用戶端的對象為代理對象,用戶端在onServiceConnected(ComponentName name, IBinder service)方法引用該對象時不能直接強轉成介面類型的執行個體,而應該使用asInterface(IBinder iBinder)進行類型轉換。
三、效果示範