是否可以从Dalvik VM中访问框架中的Android服务?
我想在Dalvik VM的某些类中进行修改(例如libcore / luni类),并希望从服务中获取结果(使用数据库和其他操作)。
这可能吗?
答案 0 :(得分:1)
请记住,框架不会在Dalvik运行的所有可能上下文中运行/设置。
那就是说,作为一个快速的黑客你可以使用反射来获得框架类。
然而,正确的方法是在Dalvik核心中定义一个API,包括接口和静态方法,其中静态方法为核心库注册接口实例。然后,在框架中,添加代码以调用该注册函数。像这样的东西(这里非常简化,例如你需要错误/权限检查):
在libcore中:
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()
注册它。