0

这通常是我所拥有的:

class A extends Activity{
  // On a click, starts service B.
}

class B extends IntentService{
    C c = new C();
    c.init();
}

class C {
    public void init(){
        // Read a file from the /raw directory.
    }
}

在互联网上搜索时,我发现大量资源说要使用 getAssets。但是,我不能这样做,因为 C 没有扩展 Activity,因此看不到 A 的上下文。我想把它传下来,但我不能这样做,因为 A 在 Class 对象上而不是 B 本身上做了一个 startService。有没有办法简单地做到这一点(最好是获取 raw/ 文件夹的 URL),还是我必须做一些 Java 魔法才能让它工作?提前致谢。

4

1 回答 1

0

只需将您的IntentServiceas传递this给 C 类......

class B extends IntentService{
    C c = new C();
    c.init(this);
}

class C {
    public void init(Context context){
        // Use the context parameter to access your resources here
        // Read a file from the /raw directory.
    }
}
于 2012-06-28T17:02:00.747 回答