The aidl implementation can call the method of the server in the client and transmit data to the server, or the server can transmit data. However, if you want to call the method of the client from the server, then you need to register callback!
Copy From and source code: http://zxl-ong.iteye.com/blog/736888
The preceding file describes how to use aidl to implement inter-process communication, but only the client calls the server method. Now let's take a look at the server's call callback to the client method!
The remotecallbacklist class is used for client calls by the server;
I. Server
First, you must write an aidl file called by the client to the server:
Package net. blogjava. mobile. complex. type. aidl; import net. blogjava. mobile. complex. type. aidl. product; import net. blogjava. mobile. complex. type. aidl. itaskcallback; interface imyservice {// method map getmap (in string country, in product) called by the client to the server; product getproduct (); void getproduc (out product ); // registers the callback object void registercallback (itaskcallback CB); void unregistercallback (itaskcallback CB );}
This file will generate a. Java file;
Then create a new class and extend service for aidl services. Rewrite the onbind () function in the service and implement the methods in aidl:
Public class myserviceimpl extends imyservice. stub {@ overridepublic product getproduct () throws RemoteException {product Product = new product (); product. setid (1234); product. setname ("Auto"); product. setprice (31000); return product ;}@ overridepublic void getproduc (product) throws RemoteException {If (Product = NULL) Product = new product (); product. setid (1234); product. setname (""); product. setprice (62000) ;}@ overridepublic map getmap (string country, product) throws RemoteException {map = new hashmap <string, string> (); map. put ("country", country); map. put ("ID", product. GETID (); map. put ("name", product. getname (); map. put ("price", product. getprice (); map. put ("product", product); Return map;} @ overridepublic void registercallback (itaskcallback CB) throws RemoteException {If (CB! = NULL) mcallbacks. Register (CB) ;}@ overridepublic void unregistercallback (itaskcallback CB) throws RemoteException {If (CB! = NULL) mcallbacks. unregister (CB) ;}@ overridepublic ibinder onbind (intent) {return New myserviceimpl ();} private remotecallbacklist <itaskcallback> mcallbacks;
Mcallbacks. Register (CB); is a method of remotecallbacklist. The client must call this method to register the callback function after obtaining the service;
After the client is successfully registered, you can call the callback function using the following methods:
Void callback (INT startid) {// return the number of callback functions in boardcast. Final int n = mcallbacks. beginbroadcast (); log. I (TAG, "mcallbacks. beginbroadcast (): "+ n); For (INT I = 0; I <n; I ++) {log. I (TAG, "I =" + String. valueof (I); try {mcallbacks. getbroadcastitem (I ). clienttackcallback (startid);} catch (RemoteException e) {// The remotecallbacklist will take care of removing // The dead object for us .}} mcallbacks. finishbroadcast ();}
Mcallbacks. beginbroadcast (); returns the number of registered callback functions;
Ii. Client
The client must first copy the aidl file, or directly copy the generated GEN/. Java to SRC;
First, get the process share:
bindService(new Intent("net.blogjava.mobile.complex.type.aidl.IMyService"),serviceConnection, Context.BIND_AUTO_CREATE);
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override public void onServiceConnected(ComponentName name, IBinder service) { myService = IMyService.Stub.asInterface(service); try { myService.registerCallback(mCallback); } catch (RemoteException e) { e.printStackTrace(); } btnInvokeAIDLService.setEnabled(true);
}
@Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub
}
You must register a callback function when obtaining the service on the server. The following is the implementation of callback:
private ITaskCallback mCallback = new ITaskCallback.Stub() {@Overridepublic void clientTackCallBack(int actionId) throws RemoteException {Log.i(TAG, "actionId :" + actionId);} };
In this way, the clienttackcallback () function is called on the server to implement the client callback;