Android - 未调用反射接口
Android - reflection interface not called
这是创建 ServiceConnection 接口的常规方法。如果我这样做,一切正常。
ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
但是如果我像下面这样使用反射定义一个 ServiceConnection 接口,那么唯一调用的方法是 hashCode()
.
ServiceConnection mServiceConnection = (ServiceConnection) java.lang.reflect.Proxy.newProxyInstance(
ServiceConnection.class.getClassLoader(),
new java.lang.Class[] { ServiceConnection.class },
new java.lang.reflect.InvocationHandler() {
@Override
public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws java.lang.Throwable {
log("methodName", method.getName());
return null;
}
});
用法是这样的:
applicationContext.bindService(serviceIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
我做错了什么?
更新
好吧,这很奇怪...我使用了调试器,在 the line bindService()
上,调试器显示 mServiceConnection 为空,而在上一行中我写了 log(Boolean.toString(mServiceConnection == null))
并打印了错误(!!? ?!!)。然后我把invoke方法的return值改成return 1;
@Override
public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws java.lang.Throwable {
log("methodName", method.getName());
return 1;
}
现在它可以工作了...同时这个接口的两个方法都是无效的。这里发生了什么?
问题是 invoke 方法返回了 null。显然在系统方法中,在某些时候调用了 hashCode() 方法并且 null 值是一个问题。
ServiceConnection mServiceConnection = (ServiceConnection) java.lang.reflect.Proxy.newProxyInstance(
ServiceConnection.class.getClassLoader(),
new java.lang.Class[] { ServiceConnection.class },
new java.lang.reflect.InvocationHandler() {
@Override
public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws java.lang.Throwable {
log("methodName", method.getName());
return 1;
}
});
这样就可以了。
这是创建 ServiceConnection 接口的常规方法。如果我这样做,一切正常。
ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
但是如果我像下面这样使用反射定义一个 ServiceConnection 接口,那么唯一调用的方法是 hashCode()
.
ServiceConnection mServiceConnection = (ServiceConnection) java.lang.reflect.Proxy.newProxyInstance(
ServiceConnection.class.getClassLoader(),
new java.lang.Class[] { ServiceConnection.class },
new java.lang.reflect.InvocationHandler() {
@Override
public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws java.lang.Throwable {
log("methodName", method.getName());
return null;
}
});
用法是这样的:
applicationContext.bindService(serviceIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
我做错了什么?
更新
好吧,这很奇怪...我使用了调试器,在 the line bindService()
上,调试器显示 mServiceConnection 为空,而在上一行中我写了 log(Boolean.toString(mServiceConnection == null))
并打印了错误(!!? ?!!)。然后我把invoke方法的return值改成return 1;
@Override
public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws java.lang.Throwable {
log("methodName", method.getName());
return 1;
}
现在它可以工作了...同时这个接口的两个方法都是无效的。这里发生了什么?
问题是 invoke 方法返回了 null。显然在系统方法中,在某些时候调用了 hashCode() 方法并且 null 值是一个问题。
ServiceConnection mServiceConnection = (ServiceConnection) java.lang.reflect.Proxy.newProxyInstance(
ServiceConnection.class.getClassLoader(),
new java.lang.Class[] { ServiceConnection.class },
new java.lang.reflect.InvocationHandler() {
@Override
public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws java.lang.Throwable {
log("methodName", method.getName());
return 1;
}
});
这样就可以了。