android - Invoke a method in Singleton class using reflection in java -
i have following class:
public class abcinfo { private static abcinfo instance = null; public static abcinfo getinstance(param param); // instance private abcinfo(param); // making singleton class public void methoda(param1 param1); // need invoke method }
using reflection in java, how can invoke methoda()
. writing android application, want use method in existing(assume present time) application in phone.
below things have tried:
string apkname = activity.getpackagemanager().getapplicationinfo(packagename, 0).sourcedir; pathclassloader mypathclassloader = new dalvik.system.pathclassloader( apkname, classloader.getsystemclassloader()); class<?> handler = class.forname(classname, true, mypathclassloader); method m0 = handler.getdeclaredmethod("getinstance", new class[] { param.class }); m0.setaccessible(true); object b = m0.invoke(null, new object[]{ param}); method m = handler.getmethod("methoda", new class[] { param1.class}); param1 methodparameter = "asdf"; object b1 = m.invoke(b, new object[] { methodparameter });
but still not working.
i found solution , below
object b1 = m.invoke(handler.newinstance(), new object[] { methodparameter });