Android筆記三十四.Service綜合實例二
阿新 • • 發佈:2017-06-17
com err out 生成 lds fcm con .so ron
綜合實例2:client訪問遠程Service服務
實現:通過一個button來獲取遠程Service的狀態,並顯示在兩個文本框中。
思路:如果A應用須要與B應用進行通信,調用B應用中的getName()、getAuthor()方法,B應用以Service方式向A應用提供服務。所以。我們能夠將A應用看成是client,B應用為服務端,分別命名為AILDClient、AILDServer.
一、服務端應用程序 1.src/com.example.aildserver/song.aidl:AILD文件 當完畢aidl文件創建後,選擇保存,eclipse會自己主動在項目的gen文件夾中同步生成Song.java接口文件。接口文件裏生成一個Stub抽象類,裏面包含aidl定義的方法。還包含一些其他 輔助性的方法,如geName()、getSong()方法,我們能夠通過這兩個方法實現client讀寫Service服務端數據。
編寫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,不能為其他標記。
二、client應用程序
1.拷貝服務端.aidl文件到client 把AIDLService應用中aidl文件所在package連同aidl文件一起復制到clientAIDLClient應用,eclipse會自己主動在A應用的gen文件夾中為aidl文件同步生成Song.java接口文件,接下來就能夠在AIDLClient應用中實現與AIDLService應用通信。 2.src/com.example.aildclient/MainActivity.java 功能:(1)啟動服務端Service服務;(2)獲取返回的IBinder代理對象,並完畢與服務端程序的通信
一、服務端應用程序 1.src/com.example.aildserver/song.aidl:AILD文件 當完畢aidl文件創建後,選擇保存,eclipse會自己主動在項目的gen文件夾中同步生成Song.java接口文件。接口文件裏生成一個Stub抽象類,裏面包含aidl定義的方法。還包含一些其他
- package com.example.aildserver;
- interface Song
- {
- String getName();
- String getSong();
- }
2.src/com.example.aildserver/MyService.java 功能:Service子類。完畢Service服務 開發核心步驟: (1)重寫Service的onBind()方法(用於返回一個IBinder對象)、onCreate()方法、onDestroy() 方法、onUnbind()方法; (2)定義一個Stub的子類。該內部類實現了IBinder、Song兩個接口。該子類對象將作為遠程Service的onBind()方法返回IBinder對象的代理傳給client的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.client回調該方法獲取歌手名
- public String getName() throws RemoteException
- {
- return name;
- }
- //b.client回調該方法獲取歌曲
- public String getSong() throws RemoteException
- {
- return song;
- }
- }
- /*1.onBind方法
- * service用於返回一個IBinder對象給client方便通信
- */
- @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;
- }
- }
註意2:與綁定本地Service不同的是,本地Service的onBind()方法會直接把IBinder對象本身傳給client的ServiceConnection的onServiceConnected方法的第二個參數。
但遠程Service的onBind()方法僅僅是將IBinder對象的代理傳給client的ServiceConnection的onServiceConnected方法的第二個參數。
當client獲取了遠程的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>
二、client應用程序
1.拷貝服務端.aidl文件到client 把AIDLService應用中aidl文件所在package連同aidl文件一起復制到clientAIDLClient應用,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();
- }
- }
- });
- }
- }
Android筆記三十四.Service綜合實例二