0

This is generally what I have:

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.
    }
}

Doing Internet searching, I found swaths of resources that said to use getAssets. However, I'm unable to do that since C does not extend an Activity, and, thus, can't see A's context. I thought of passing it down, but I'm not able to do that since A does a startService on a Class object and not on B itself. Is there a way to do this simply (preferably getting the darn URL for the raw/ folder) or do I have to do some Java magic to get this to work? Thanks in advance.

i41
  • 311
  • 2
  • 9
  • You can always get the application context. Just extend the android.app.Application and grab the instance inside a constructor. Oh, and add it to tag in AndroidManifest.xml – nullpotent Jun 28 '12 at 16:56
  • Check out my answer [here](http://stackoverflow.com/questions/9503114/android-test-project-reading-assets-file-to-test-plain-java-object/9506449#9506449) to see how to read assets from POJO. – yorkw Jun 28 '12 at 23:28

1 Answers1

0

Just pass your IntentService as this to class 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.
    }
}
Squonk
  • 48,735
  • 19
  • 103
  • 135