[android] 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?

This question is related to android broadcastreceiver android-service

The answer is


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

Service is a Context, so do this:

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

  1. Service extends ContextWrapper
  2. ContextWrapper extends Context

So....

Context context = this;

(in Service or Activity Class)


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

DataBaseManager dbm = Utils.getDataManager(this);   

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; 

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


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to broadcastreceiver

Sending intent to BroadcastReceiver from adb Broadcast receiver for checking internet connection in android app Sending and Receiving SMS and MMS in Android (pre Kit Kat Android 4.4) android - How to get view from context? Broadcast Receiver within a Service How to use LocalBroadcastManager? Android - Start service on boot Get Context in a Service How do I start my app on startup? Receiver not registered exception error?

Examples related to android-service

Android 8.0: java.lang.IllegalStateException: Not allowed to start service Intent Android Fatal signal 11 (SIGSEGV) at 0x636f7d89 (code=1). How can it be tracked down? How to get the current location latitude and longitude in android Service vs IntentService in the Android platform Background service with location listener in android Launch Android application without main Activity and start Service on launching application START_STICKY and START_NOT_STICKY Broadcast Receiver within a Service How to run an android app in background? Android background music service