• Android AIDL Callback的使用(配源码)


    零、示例说明

    本示例,完成的功能是:客户端向服务端注册一个回调,服务端是一个商店shop,当商店里的产品 Product 有变化时,调用回调向通知客户端,什么商品更新了。

    一、完整源代码

    完整源码链接: https://github.com/jx0260/TestGradle

    二、实现步骤

    2.1 定义实体

    定义 Product.aidl

    // Product.aidl
    package com.example.testgradle;
    
    // Declare any non-default types here with import statements
    
    parcelable Product;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    对应的实体类:

    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];
            }
        };
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    定义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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    定义服务端 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);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2.2 Make project 生成相应的 AIDL实现类

    客户端:
    在这里插入图片描述
    服务端:
    在这里插入图片描述

    2.2 编写客户端

    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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    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
    
    • 1
    • 2
    • 3
    • 4

    IShopProductCallback.Stub 继承了 Binder,实际上此处将 IShopProductCallback 的实现传进了参数,到达服务端。服务端拿到的是 IShopProductCallback.Stub.Proxy,这里就会有一种服务端变成客户端的感觉。实际上确实如此,在使用Callback的时候 服务端对于Callback来说 确实就变成了客户端,将变化的product 提交到服务端(也就是业务上的客户端)
    此处,引用的“客户端”,“服务端”是有两套意思,一套是指我们总体业务的 客户端、服务端,另一套是指对于callback来说,程序实现的 客户端、服务端。

    2.3 编写服务端

    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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    服务端 public void registerCallback(IShopProductCallback callback) 拿到客户端传来的 callback其实是一个proxy 存到mCallback 本地变量中,20秒后,创建一个变化的Product 调用 mCallback.onProductChanged(product); 将商品信息回调给客户端。

    2.4 运行

    日志打印:

    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
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    第3章 信息收集
    今日睡眠质量记录80分
    【面试题】金九银十,你准备好面试了吗? (30w字前端面试题总结)(VUE)
    函数 function
    vue - vuex实现持久化存储
    通达OA_文件包含漏洞
    浏览器存储(webStorage)常用API以及简单使用
    基于大数据的工业设备故障诊断模型设计
    基于蛙跳算法求解简单调度问题附matlab代码
    量子随机预言机(QROM)是什么?
  • 原文地址:https://blog.csdn.net/jx0260/article/details/131082542