1> 首先定义服务器和客户端都要用到的接口:
客户通过IBankService接口调用服务器端提供的函数
+--+--+ IBankService.aidl +--+--+
package com.jxdsoft; import com.jxdsoft.IClientCallback; // 服务器回调客户的函数的话,就要用这个自定义的接口 interface IBankService { int getAccountBalance(); // 客户想查询余额 void setAccountOwner(in String name); // 客户想改账户名称 void setAccountInfo(int deposit, in String pin); // 客户想改账号信息 void showBankStatus(); // 查看服务 }
服务器也可以回调客户端定义的函数
+--+--+ IClientCallback.aidl +--+--+
package com.jxdsoft; interface IClientCallback { void actionPerformed(int actionId); }
2> 然后修改服务器端的AndroidManifest.xml。给服务起名叫BankService。
... </activity> <service android:name="BankService" android:process=":remote"> <intent-filter> <action android:name="com.lifeblood.IBankService" /> </intent-filter> </service>
3> 把接口定义的函数全部实现
+--+--+ BankService.java +--+--+
package com.jxdsoft; import com.lifeblood.IBankService.Stub; import android.content.Context; import android.content.Intent; import android.app.Service; import android.os.IBinder; import android.os.RemoteException; import android.os.RemoteCallbackList; // 想让服务器回调客户端的函数,就要用这个来管理那些回调接口 public class BankService extends Service { private Context mContext = null; private IBankService.Stub mBinder = new Stub() { private String mName = null; private int mBalance = 0; private String mPin = "1234"; int getAccountBalance() { return mBalance; } void setAccountOwner(String ow) { mName = ow; } void setAccountInfo(int deposit, String pin) { mBalance += deposit; mPin = pin; } void showBankStatus() { Intent intent = new Intent(mContext, BankActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } }; @Override public IBinder onBind(Intent intent) { mContext = this; return mBinder; //返回AIDL接口实例化对象 } @Override public void onCreate() { } @Override public void onDestroy() { super.onDestroy(); } @Override public void onStart(Intent intent, int startId) { callback(startId); } @Override public boolean onUnbind(Intent intent) { return super.onUnbind(intent); } public void onRebind(Intent intent) { super.onRebind(intent); } // 回调客户端的函数,要奏三部曲: // beginBroadcast + getBroadcastItem + finishBroadcast void callback(int val) { final int N = mCallbacks.beginBroadcast(); for (int i=0; i<N; i++) { try { mCallbacks.getBroadcastItem(i).actionPerformed(val); } catch (RemoteException e) { // The RemoteCallbackList will take care of removing // the dead object for us. } } mCallbacks.finishBroadcast(); } final RemoteCallbackList<IClientCallback> mCallbacks = new RemoteCallbackList<IClientCallback>(); }
4> 继续实现服务器的主Activity - BankActivity
package com.jxdsoft; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class BankActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent service = new Intent(this, BankService.class); startService(service); } }
5> 最后实现客户端的Activity,使用接口
package com.jxdsoft; import com.lifeblood.IBankService; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; public class AIDLBankClient extends Activity { private IBankService mService = null; private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName name, IBinder service) { // 连接到服务器时,把传来的IBinder转型为自定义的服务接口 mService = IBankService.Stub.asInterface(service); try { mService.registerCallback(mCallback); } catch (RemoteException e) { // todo } } public void onServiceDisconnected(ComponentName name) { mService = null; } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent service = new Intent(IBankService.class.getName()); bindService(service, mConnection, BIND_AUTO_CREATE); } ... // 使用mService调用getAccountBalance等接口 ... @Override protected void onDestroy() { super.onDestroy(); unbindService(mConnection); } // 给服务器调用的回调函数 private IClientCallback mCallback = new IClientCallback.Stub() { public void actionPerformed(int id) { Log.e("[CLIENT]", "Hello, client: " + id); } }; }
原文地址:http://www.cnblogs.com/tt_mc/archive/2010/05/27/1745269.html