是否可以从 Dalvik VM 访问框架中的 android 服务?
我想对 Dalvik VM 的某些类(例如 libcore/luni 类)进行修改,并希望从服务返回结果(使用数据库和其他操作)。
这可能吗?
最佳答案
请记住,框架不会在 Dalvik 运行的所有可能上下文中运行/设置。
也就是说,作为一种快速破解,您可以使用反射来获取框架类。
不过,正确的方法是在 Dalvik 核心中定义一个 API,由类似接口(interface)和静态方法的东西组成,其中静态方法注册一个接口(interface)实例供核心库使用。然后,在框架中,添加代码以调用该注册函数。像这样的东西(这里非常简化,例如你想要错误/权限检查):
在库核心中:
public interface TheInterface {
void doSomethingInteresting();
...
}
public class TheRegistrar {
private static TheInterface theOne;
public static void register(TheInterface instance) {
theOne = instance;
}
public static TheInterface get() {
return theOne;
}
}
然后,在想要使用它的 libcore 代码中,让它执行 get()
(并准备好处理它为 null
的情况)。
在框架中,定义如下内容:
public class FrameworkDoohicky implements TheInterface {
...
}
并在框架初始化期间调用 TheRegistrar.register()
进行注册。
关于android - 从 Dalvik VM 访问 android 框架服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11739348/