mercredi 18 juillet 2018

How to create a callback using reflection in android?

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.

Below is my sample code :

 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);

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) { }
};

  • 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?





Aucun commentaire:

Enregistrer un commentaire