When to use a service in Android

Posted by Computerish on Stack Overflow See other posts from Stack Overflow or by Computerish
Published on 2010-06-09T14:30:22Z Indexed on 2010/06/09 14:32 UTC
Read the original article Hit count: 155

Filed under:

Hi everyone,

I have a class that fetches data in response to button presses in the main activity. Unfortunately, I keep running into problems because this class is not an Activity or a Service. For example, without a Context I cannot translate a resource id into a string:

getString(R.string.example_string); // Doesn't work

Should I make this class into a Service and have the main Activity stop the class when it is closed? Should I pass the Context from the Activity into this class like this?

MyClass c = new MyClass(this);

Or is there some better way to handle this problem?

This issue also comes up when I try to send a Toast from this class.

© Stack Overflow or respective owner

Related posts about android