问题描述
我想拦截android视图类方法调用。例如在视图类中有一个方法名 getHeight()。所以当我的代码 obj.getHeight() 调用时,我想拦截它并想返回一个任意值。
我可以拦截我自己的类方法调用。但不能用 android 视图类做到这一点。 我正在使用使用 Byte Buddy 的 Android Buddy。所以这是我的代码
object ViewMethodInterceptor {
@JvmStatic
fun intercept(@SuperCall originalMethodCall: Runnable): Int {
Log.i("mytag","Intercepted View Class getHeight ")
return 500;
}
}
@Transformation
class ViewTransformation : Plugin {
override fun apply(
builder: DynamicType.Builder<*>,typeDescription: TypeDescription,classFileLocator: ClassFileLocator
): DynamicType.Builder<*> {
return builder.method(ElementMatchers.named("getHeight"))
.intercept(MethodDelegation.to(ViewMethodInterceptor::class.java))
}
override fun matches(target: TypeDescription): Boolean {
return target.isAssignableto(View::class.java)
}
override fun close() {
// nothing to close
}
}
我从 MainActivity 的 onCreate 方法调用 getHeight() 方法。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)