【问题标题】:Why can't use proxy to AIDL interface?为什么不能使用代理到 ​​AIDL 接口?
【发布时间】:2019-08-20 01:49:12
【问题描述】:

使用代理对象实现 AIDL 接口

    Class studyManager = Class.forName("StudyManager");
    Class iStudyCallbackClient = Class.forName("IStudyCallbackClient");
    Method setTouchCallback = studyManager.getDeclaredMethod("setTouchCallback", iStudyCallbackClient);
    Proxy proxy = (Proxy) Proxy.newProxyInstance(iStudyCallbackClient.getClassLoader(),
        new Class[] { iStudyCallbackClient }, new IStudyCallbackClientImpl());
    setTouchCallback.invoke(studyManager.newInstance(), proxy);

class IStudyCallbackClientImpl implements InvocationHandler {
    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
        Log.d(TAG, method.getName());//why here is asBinde() not sendPoint()? 
        return null;
    }

AIDL

interface IStudyCallbackClient {
    void sendPoint(int x,int y);
}

问题:

代码运行正常。
当调用回调函数(IStudyCallbackClient.sendPoint(int x,int y))时, 为什么 method.getName() 是 asBinder?而不是 sendPoint()?

AIDL 接口自动生成的代码在这里。

https://pastebin.com/CNFxGGu7

【问题讨论】:

    标签: java android dynamic-proxy


    【解决方案1】:

    错误是 JDK 的代理依赖接口。真正的 args 是实现接口的抽象类,而不仅仅是接口。

    我首先想到的接口参数是这样的:

    public void setTouchCallback(ICallback mICallback)
    

    但真正的参数是这样的

    public static abstract class Stub extends android.os.Binder implements ICallback
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多