如何在android JAVA中用反射实现隐藏界面?

发布于 2024-12-26 07:43:19 字数 1166 浏览 1 评论 0原文

我想在 android 4.0 以后使用 android.filterfw.* framworks 。但是,它仍然隐藏在SDK中。 所以,现在我正在尝试JAVA反射技巧。

有谁知道如何在android JAVA中用反射实现隐藏接口?

我想用反射来模仿下面的代码。

private SurfaceTextureSourceListener mSourceReadyCallback = new SurfaceTextureSourceListener() {
    public void onSurfaceTextureSourceReady(SurfaceTexture source) {

    }
};

mGraphEnv.addReferences("textureSourceCallback", mSourceReadyCallback);

我做了这个。

Class c = Class.forName("android.filterpacks.videosrc.SurfaceTextureSource$SurfaceTextureSourceListener"); //step 1
instance = c.newInstance(); //step 2
proxy = Proxy.newProxyInstance(instance.getClass().getClassLoader(), new Class[] { instance.getClass() }, mHandler); //step 3

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

    //TODO

    return method.invoke(instance, args);
   }
};

然后,我在“第2步”上遇到了例外。好的,我知道我无法创建接口目录的实例。 所以,我的问题是如何用反射实现隐藏接口。

我不能做这样的事情...

public class MyIPL implements SurfaceTextureSourceListener {

}

你知道有什么想法吗?

I would like to use android.filterfw.* framworks in android 4.0 later. but, it is still hidden in SDK.
so, now I am trying JAVA reflection-tricks.

do anyone know how to implement hidden interface with reflection in android JAVA ?

I want to mimic the following code with reflection.

private SurfaceTextureSourceListener mSourceReadyCallback = new SurfaceTextureSourceListener() {
    public void onSurfaceTextureSourceReady(SurfaceTexture source) {

    }
};

mGraphEnv.addReferences("textureSourceCallback", mSourceReadyCallback);

I did this one.

Class c = Class.forName("android.filterpacks.videosrc.SurfaceTextureSource$SurfaceTextureSourceListener"); //step 1
instance = c.newInstance(); //step 2
proxy = Proxy.newProxyInstance(instance.getClass().getClassLoader(), new Class[] { instance.getClass() }, mHandler); //step 3

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

    //TODO

    return method.invoke(instance, args);
   }
};

then, I got an exception on "step 2". OK, I understand I can't create an instance of interface directory.
so, my question is how to implement hidden interface with reflection.

I can not do something like this...

public class MyIPL implements SurfaceTextureSourceListener {

}

do you know any ideas ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文