赞
踩
本示例,完成的功能是:客户端向服务端注册一个回调,服务端是一个商店shop,当商店里的产品 Product 有变化时,调用回调向通知客户端,什么商品更新了。
完整源码链接: https://github.com/jx0260/TestGradle
定义 Product.aidl
// Product.aidl
package com.example.testgradle;
// Declare any non-default types here with import statements
parcelable Product;
对应的实体类:
package com.example.testgradle; import android.os.Parcel; import android.os.Parcelable; public class Product implements Parcelable { private String no; private String name; public String getNo() { return no; } public void setNo(String no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(this.no); dest.writeString(this.name); } public void readFromParcel(Parcel source) { this.no = source.readString(); this.name = source.readString(); } public Product() { } protected Product(Parcel in) { this.no = in.readString(); this.name = in.readString(); } public static final Creator<Product> CREATOR = new Creator<Product>() { @Override public Product createFromParcel(Parcel source) { return new Product(source); } @Override public Product[] newArray(int size) { return new Product[size]; } }; }
定义Callback AIDL,此 IShopProductCallback 将会注册到服务端去。
// IShopProductCallback.aidl
package com.example.testgradle;
import com.example.testgradle.Product;
// Declare any non-default types here with import statements
interface IShopProductCallback {
oneway void onProductChanged(in Product product);
}
定义服务端 IShopAidlInterface.aidl,里面有一个registerCallback的方法
// IShopAidlInterface.aidl
package com.example.testgradle;
import com.example.testgradle.IShopProductCallback;
// Declare any non-default types here with import statements
interface IShopAidlInterface {
String getProductInfo(int productNo);
void buyProduct1(int productNo, String address);
oneway void registerCallback(in IShopProductCallback callback);
}
客户端:
服务端:
Intent serviceIntent = new Intent("com.example.testgradle.ShopService"); serviceIntent.setPackage("com.example.testgradle"); bindService(serviceIntent, new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.i(TAG, "onServiceConnected() service=" + service); iShopAidlInterface = IShopAidlInterface.Stub.asInterface(service); try { Log.i(TAG, "Client: register Stub to Server!"); iShopAidlInterface.registerCallback(new IShopProductCallback.Stub() { @Override public void onProductChanged(Product product) throws RemoteException { Log.i(TAG, "Client: received product changed -- " + product.getNo() + " name:" + product.getName()); } }); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { } }, BIND_AUTO_CREATE);
bindService() com.example.testgradle.ShopService ,在 onServiceConnected 回调中,使用拿到的 iShopAidlInterface 对象,调用 registerCallback,注意,此处传入的 callback 是 IShopProductCallback.Stub
public interface IShopProductCallback extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder
IShopProductCallback.Stub 继承了 Binder,实际上此处将 IShopProductCallback 的实现传进了参数,到达服务端。服务端拿到的是 IShopProductCallback.Stub.Proxy,这里就会有一种服务端变成客户端的感觉。实际上确实如此,在使用Callback的时候 服务端对于Callback来说 确实就变成了客户端,将变化的product 提交到服务端(也就是业务上的客户端)
此处,引用的“客户端”,“服务端”是有两套意思,一套是指我们总体业务的 客户端、服务端,另一套是指对于callback来说,程序实现的 客户端、服务端。
package com.example.testgradle; import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import javax.security.auth.login.LoginException; public class ShopService extends Service { private String TAG = "ShopService"; private IShopProductCallback mCallback; public ShopService() { } private IShopAidlInterface.Stub mBinder = new IShopAidlInterface.Stub() { @Override public void registerCallback(IShopProductCallback callback) throws RemoteException { Log.i(TAG, "Server: registerCallback() this callback obj is a proxy!"); mCallback = callback; mHandler.postDelayed(new Runnable() { @Override public void run() { Product product = new Product(); product.setNo("No.0001"); product.setName("banana"); try { Log.i(TAG, "Server: the proxy obj invoke onProductChanged, pass the product to the client!"); mCallback.onProductChanged(product); } catch (RemoteException e) { e.printStackTrace(); } } }, 20000); } }; @Override public IBinder onBind(Intent intent) { return mBinder; } }
服务端 public void registerCallback(IShopProductCallback callback) 拿到客户端传来的 callback其实是一个proxy 存到mCallback 本地变量中,20秒后,创建一个变化的Product 调用 mCallback.onProductChanged(product); 将商品信息回调给客户端。
日志打印:
com.example.testgradle I/ShopService: Server: registerCallback() this callback obj is a proxy!
com.example.testgradle I/ShopService: Server: the proxy obj invoke onProductChanged, pass the product to the client!
com.example.clientdemo I/ClientActivity: Client: received product changed -- No.0001 name:banana
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。