如何在Android中使用反射创建回调? [英] How to create a callback using reflection in android?
问题描述
我正在尝试使用反射访问方法,并且参数之一是回调.回调类型是不同类类型的通用接口.这些类 @SystemApi
可以通过反射来访问.此处是我上面所说的那门课.
I am trying to access a method using reflection and one of the parameter is a callback. The callback type is generic interface of different class type. These classes are @SystemApi
can be accessed by reflection. Here is the class I am working with above sayings.
下面是我的示例代码:
String sClassName = "android.telephony.euicc.EuiccCardManager";
Class classToInvestigate = Class.forName(sClassName);
Class interfaceclass = classToInvestigate.getClasses()[0]; //android.telephony.euicc.EuiccCardManager.ResultCallback
Method getallprofiles = classToInvestigate.getDeclaredMethod("requestAllProfiles", String.class,Executor.class, interfaceclass);
getallprofiles.invoke(null,getEid(), AsyncTask.THREAD_POOL_EXECUTOR,null);
在上面的 invoke
签名中,作为最后一个参数,我需要传递使用反射创建的回调,该回调与下面的回调示例等效或相似.
In the above invoke
signature as a last parameter I need to pass the callback created using reflection and which is equivalent or similar to the below callback sample.
ResultCallback<EuiccProfileInfo[]> callback =
new ResultCallback<EuiccProfileInfo[]>() {
@Override
public void onComplete(int resultCode, EuiccProfileInfo[] result) { }
};
- 上面的ResultCallback接口可以在我上面提供的相同的类链接中,也可以在上面的
interfaceclass
字段中的相同实例中找到. - EuiccProfileInfo是另一个需要从反射访问的类,因为它是
@SystemApi
- The above ResultCallback interface can be found in the same class link I provided above and the same instance in
interfaceclass
field above. - EuiccProfileInfo is the another class need to access from reflection because it is
@SystemApi
我无法/卡住以反思的方式翻译上述回调逻辑.有人可以帮我吗?
I have failed/stuck to translate the above callback logic with reflection.Anyone can help me on this?
推荐答案
似乎需要创建代理类:
// that class you don't have access too. imagine that it is not here, it is just to show example signature of method we want to run.
interface Callback {
void run();
}
public static void main(String[] args) throws Exception {
Class<?> callbackClass = Class.forName("package.Callback");
Callback callback = Proxy.newProxyInstance(Main.class.getClassLoader(), new Class[]{callbackClass}, new CallbackInvocationHandler(() -> {
System.out.println("callback");
}));
callbackClass.getMethod("run").invoke(callback); // works!
}
static class CallbackInvocationHandler implements InvocationHandler {
private final Runnable myCallback;
CallbackInvocationHandler(Runnable myCallback) {
this.myCallback = myCallback;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("toString") && (method.getParameterCount() == 0)) {
// just random implementation
return proxy.getClass() + "@" + Integer.toHexString(System.identityHashCode(proxy));
}
if (method.getName().equals("hashCode") && (method.getParameterCount() == 0)) {
return System.identityHashCode(proxy);
}
if (method.getName().equals("equals") && (method.getParameterCount() == 1) && (method.getParameterTypes()[0] == Object.class)) {
return proxy == args[0];
}
if (method.getName().equals("run") && (method.getParameterCount() == 0)) {
// do what you want.
myCallback.run();
return null;
}
throw new IllegalStateException("Method not implemented: " + method);
}
}
但是,如果您确实需要使用类似的东西-很有可能您做错了事,难道您不可以仅添加对该项目的依赖吗?但是您也不应该依赖系统类.
But if you really need to use something like that - there are huge chances you are doing something wrong, can't you just add dependency on that project? But you should not depend on system classes too.
这篇关于如何在Android中使用反射创建回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!