311

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?

senshin
  • 10,022
  • 7
  • 46
  • 59
user123321
  • 12,593
  • 11
  • 52
  • 63

7 Answers7

863

Service is a Context

mibollma
  • 14,959
  • 6
  • 52
  • 69
  • 4
    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. – ctrl-alt-delor Jan 31 '13 at 09:19
  • 19
    Note: The context within the service will not be visible until onStart or onStartCommand in services: http://stackoverflow.com/questions/7619917/how-to-get-context-in-android-service-class – class Aug 15 '13 at 20:55
75

Service extends ContextWrapper which extends Context. Hence the Service is a Context. Use 'this' keyword in the service.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
user2138983
  • 1,313
  • 9
  • 15
35
  1. Service extends ContextWrapper
  2. ContextWrapper extends Context

So....

Context context = this;

(in Service or Activity Class)

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Hardik Gajera
  • 1,351
  • 1
  • 14
  • 21
11

just in case someone is getting NullPointerException, you need to get the context inside onCreate().

Service is a Context, so do this:

private Context context;

@Override
public void onCreate() {
    super.onCreate();
    context = this;
}

Note:

Read: "Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)" Do you know what context classes are? Activity is one of them, and you should not store Activity as a static field, (or it will leak memory). However, you can store Context (as long as it is the application context) as a static field, since it outlives everything.

8

Since Service is a Context, the variable context must be this:

DataBaseManager dbm = Utils.getDataManager(this);   
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
5

As Service is already a Context itself

you can even get it through:

Context mContext = this;

OR

Context mContext = [class name].this;  //[] only specify the class name
// mContext = JobServiceSchedule.this; 
Ali Azaz Alam
  • 1,782
  • 1
  • 16
  • 27
0

If you want service context then use this keyword. if you want activity context you can access that by using by making static variable like this

public static Context context;

in the activity onCreate()

context=this;

now you can access that context inside the service

Saurabh Dhage
  • 1,478
  • 5
  • 17
  • 31
  • Read: "Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)" Do you know what context classes are? Activity is one of them, and you should not store Activity as a static field, (or it will leak memory). However, you can store Context (as long as it is the application context) as a static field, since it outlives everything. – Ecclesiaste Panda Jun 29 '22 at 10:03