使用:
(Boolean) invokeMethod(LockPatternUtils.class, mLockutils,
"savedPasswordExists", new Class[] {int.class}, new Object[] {UserHandle.myUserId()});
(Boolean) invokeMethod(LockPatternUtils.class, mLockutils, "checkPattern",
new Class[] {List.class, int.class}, new Object[] {null, UserHandle.myUserId()});
方法实现:
public static Method getMethod(Class<?> cls, String methodName, Class<?>
parameterTypes) {
try {
return cls.getDeclaredMethod(methodName, parameterTypes);
}
catch (NoSuchMethodException e) {
Log.e(TAG, "getMethod() Exception: ", e);
try {
return cls.getMethod(methodName, parameterTypes);
}
catch (NoSuchMethodException ex) {
Log.e(TAG, "getMethod() Exception: ", ex);
return null;
}
}
}
public static Object invokeStaticMethod(Class<?> cls, String methodName) {
return invokeMethod(cls,
null, methodName,
null,
null);
}
public static Object invokeStaticMethod(Class<?> cls, String methodName, Class<?>[] parasTypes,
Object[] parasObjs) {
return invokeMethod(cls,
null, methodName, parasTypes, parasObjs);
}
public static Object invokeMethod(Class<?> cls, Object obj, String methodName) {
return invokeMethod(cls, obj, methodName,
null,
null);
}
public static Object invokeMethod(Class<?> cls, Object obj, String methodName, Class<?>[] parasTypes,
Object[] parasObjs) {
Method method = getMethod(cls, methodName, parasTypes);
try {
if (method !=
null) {
method.setAccessible(
true);
return method.invoke(obj, parasObjs);
}
}
catch (Exception e) {
Log.e(TAG, "invokeStaticMethod() Exception: ", e);
}
return null;
}