Android getContext on a background Service
Service extends Context. You can use this
, where this
is the reference to the Service
instance.
Putting more details on my comment below regarding the following code of SubscribeService class:
@Override
public void onCreate() {
super.onCreate();
context = this;
context = MyApp.getContext();
}
In your Service
's onCreate()
context = this
cannot be null
by a fundamental programming paradigm.
Try this:
Added super.onCreate();
before MyApp.context = getApplicationContext();
public class MyApp extends Application {
private static Context context;
public void onCreate() {
super.onCreate();
MyApp.context = getApplicationContext();
}
public static Context getAppContext() {
return MyApp.context;
}
}
Edit: Calling MyApp.getAppContext()
will return the application Context
.