隐藏界面的反射

Reflection of hide interface

如何调用registerAudioPortUpdateListener
调用隐藏函数成功
但是,在这种情况下,我需要调用一个带有隐藏内部接口作为参数的函数。

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);
    }
}

你能试试这个代码吗,对我来说它在 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;
        }

    }

}