为什么不能使用代理到AIDL接口?

时间:2019-08-20 01:49:12

标签: java android dynamic-proxy

使用代理对象实现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

1 个答案:

答案 0 :(得分:0)

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

我首先认为接口args是这样的:

public void setTouchCallback(ICallback mICallback)

但是真正的参数就像这样

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