mercredi 27 septembre 2017

Android - define interface with reflection not working

This is the normal way of creating a ServiceConnection interface. If I do it this way everything works fine.

  ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

But if I define a ServiceConnection interface using reflection like below then the only method ever called is 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;
        }
});

The usage is this:

mContext.bindService(serviceIntent, mServiceConnection, Context.BIND_AUTO_CREATE);

What am I doing wrong here?





Aucun commentaire:

Enregistrer un commentaire