One alternative is to look for, or create, a public singleton.
A public singleton is a class that (1) ever only has one instance,
and (2) that instance can be located from anywhere by means of a
static method.

One candidate for singleton status is Application -- it is a Context,
and you expect there'll only be one of them.  In your extension of
Application, have the constructor save 'this' to a static field, and
have a public static method to get the saved reference.  Voi la, you
can now get a useful Context from anywhere.  Most apps don't use an
Application extension, but you can use this pattern on pretty much
any class.

On Mar 4, 9:38 am, Jake Colman <[email protected]> wrote:
> I'm sure that this is a basic question that must have been answered a
> 100 times but I googled to no avail.  Maybe I am going about this the
> wrong way.
>
> I am creating a class that is not an Activity or a Service.  I need that
> class to be able to get a hold of, for example, the Location Manager.
> If I had a context I would call
>
>    (LocationManger)context.getSystemService(...)
>
> How do I obtain a context in this situation?
>
> I would prefer not passing in the context to the class constructor.  Am
> I fundamentally misunderstanding this environment?
>
> --
> Jake Colman -- Android Tinkerer

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to