我正在尝试在从系统服务获取其实例的私有字段上调用方法。问题是我无法在私有字段上调用该方法,并且我一直收到IllegalArgumentExceptions。
Interface MyRequiredService {
public void setValues(int a, int b, String c);
}
public Class ARandomClass {
private MyRequiredService mService;
private synchronized MyRequiredService getServiceInstance() {
mService = getInternalSystemService();
return mService;
}
}
我的代码:
private void testingIstatusBar() {
Object object = getSystemService("internalSystemService");
Class<?> randomClass;
try {
randomClass = Class.forName("com.system.ARandomClass ");
Field mService = randomClass.getDeclaredField("mService");
mService.setAccessible(true);
Class<?> myRequiredService = mService.getType();
Method getService = myRequiredService.getMethod("setValues",
new Class[] { int.class, int.class, String.class });
getService.setAccessible(true);
getService.invoke((Object)mService.getType(), 0, 1,
"OnSystemUiVisibilityChangeListener");
} catch (Exception e) {
Log.e(TAG, " Exception"+ e.getLocalizedMessage());
}
}
例外:
java.lang.IllegalArgumentException: Expected receiver of type com.system.internal.MyRequiredService, but got java.lang.Class<com.system.internal.MyRequiredService>
如何调用 setValue() 方法?我做错了什么?
答案 0 :(得分:1)
你必须调用真实对象而不是类。要获得真实对象,您应该致电:
getService.invoke(mService.get(object), 0, 1,
"OnSystemUiVisibilityChangeListener");