2017-06-28 5 views
0

Comment puis-je appeler registerAudioPortUpdateListener?
J'ai réussi à appeler une fonction cachée.
Mais, dans cette situation, j'ai besoin d'appeler une fonction avec une interface interne cachée comme paramètre.Réflexion de l'interface de masquage

public class AudioManager { 
    /** 
    * Listener registered by client to be notified upon new audio port connections, 
    * disconnections or attributes update. 
    * @hide 
    */ 
    public interface OnAudioPortUpdateListener { 
     /** 
     * Callback method called upon audio port list update. 
     * @param portList the updated list of audio ports 
     */ 
     public void onAudioPortListUpdate(AudioPort[] portList); 

     /** 
     * Callback method called upon audio patch list update. 
     * @param patchList the updated list of audio patches 
     */ 
     public void onAudioPatchListUpdate(AudioPatch[] patchList); 

     /** 
     * Callback method called when the mediaserver dies 
     */ 
     public void onServiceDied(); 
    } 


    /** 
    * Register an audio port list update listener. 
    * @hide 
    */ 
    public void registerAudioPortUpdateListener(OnAudioPortUpdateListener l) { 
     sAudioPortEventHandler.init(); 
     sAudioPortEventHandler.registerListener(l); 
    } 
} 
+0

si quelque chose est privé en java, vous êtes soit pas censé y avoir accès à tout, ou que vous êtes censé faire d'une autre manière, comme prévu par le –

+0

dev je sais. Mais j'insiste pour appeler cette fonction. – motis10

Répondre

0

Pouvez-vous essayer ce code, pour moi, il travaille en java,

import java.lang.reflect.InvocationHandler; 
import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 
import java.lang.reflect.Proxy; 

import de.apps.io.AudioManager; 


public class Application { 

    public static void main(String[] args) { 
     System.out.println("hello world"); 



     Class someInterface1 = null; 
     try { 
     // someInterface = AudioManager.class.getDeclaredClasses(); 

      someInterface1 = Class.forName("de.apps.io.AudioManager$OnAudioPortUpdateListener"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     //System.out.println(someInterface); 


     System.out.println(someInterface1); 



     Object o = Proxy.newProxyInstance(someInterface1.getClassLoader(), new java.lang.Class[] { someInterface1 }, new Handler()); 


     AudioManager manager = new AudioManager(); 
     Method me = null; 
     try { 
      me = manager.getClass().getMethod("registerAudioPortUpdateListener", new java.lang.Class[] { someInterface1 }); 
     } catch (NoSuchMethodException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (SecurityException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      me.invoke(manager, o); 
     } catch (IllegalAccessException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalArgumentException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InvocationTargetException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     System.out.println(me); 

    } 



    static class Handler implements InvocationHandler 
    { 

     @Override 
     public Object invoke(Object proxy, Method method, Object[] args) 
       throws Throwable { 

      String method_name = method.getName(); 
      Class<?>[] classes = method.getParameterTypes(); 

      System.out.println("called " + method_name); 

      return null; 
     } 

    } 

}