How to get Context in an Intent Service How to get Context in an Intent Service android android

How to get Context in an Intent Service


So my question is how do I get the context in an IntentService?

The IntentService is the Context, as IntentService inherits from Context.


Just call getApplicationContext()


You can get the context in onStartCommand() function.

    public class ExampleService extends IntentService {    private Context mContext;    public ExampleService(String name) {    super(name);    }    @Override    protected void onHandleIntent(Intent intent) {    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {    mContext = getApplicationContext();    return super.onStartCommand(intent, flags, startId);    }    }