Get Context in a Service

Is there any reliable way to get a Context from a Service ? I want to register a broadcast receiver for ACTION_PHONE_STATE_CHANGED but I don't need my app to always get this information, so I don't put it in the Manifest . However, I can't have the broadcast receiver be killed by the GC when I need this information so I'm registering the broadcast receiver in a Service . Hence, I need a Context to to call registerReceiver() . When I no longer need the ACTION_PHONE_STATE_CHANGED I unregister it. Any tips?

10.3k 7 7 gold badges 48 48 silver badges 60 60 bronze badges asked Jun 22, 2011 at 20:33 user123321 user123321 12.8k 12 12 gold badges 53 53 silver badges 63 63 bronze badges

7 Answers 7

answered Jun 22, 2011 at 20:37 15.1k 6 6 gold badges 53 53 silver badges 71 71 bronze badges

I had this problem, but it turned out that it was the worker thread that had no context. I solved it my passing a context when constructing the thread.

Commented Jan 31, 2013 at 9:19

Note: The context within the service will not be visible until onStart or onStartCommand in services: stackoverflow.com/questions/7619917/…