I have a class which extends android.app.Application
, which I use to persist global state around my application.
I want to start off a service when my application starts, so inside the constructor of this GlobalState
class I try to create an intent and start a service, but I can't create an intent because I can't get hold of Context
public GlobalState() {
Log.d(this.getClass().getSimpleName(), "Initialise DatabaseManager");
dbManager = new DatabaseManager(this);
Log.d(this.getClass().getSimpleName(), "Requesting start up of ContactsUpdater Service");
Intent i = new Intent(this, ContactsUpdater.class);
startService(i);
}
I've tried using getApplicationContext()
, but this throws a null pointer exception.
java.lang.NullPointerException at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120) at android.content.ComponentName.(ComponentName.java:75) at android.content.Intent.(Intent.java:2551) at com.jameselsey.apps.cercademi.domain.GlobalState.(GlobalState.java:48) at java.lang.Class.newInstanceImpl(Native Method) at java.lang.Class.newInstance(Class.java:1479) at android.app.Instrumentation.newApplication(Instrumentation.java:957) at android.app.Instrumentation.newApplication(Instrumentation.java:942) at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:518)
I'm confused, I can create the DatabaseManager
fine using this..
Any ideas?