Reputation: 16428
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?
Upvotes: 2
Views: 2391
Reputation: 344
Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's <application> tag, which will cause that class to be instantiated for you when the process for your application/package is created.
Source: http://developer.android.com/reference/android/app/Application.html
Four times out of ten, an Android developer's force close problem is forgetting to modify the Android manifest. (I totally made up that statistic).
Application
extends Context
, so you should be able to pass it.
However, extending Application is not the most resource-efficient way of doing this. To explain it more succinctly, here's a nother quote from the same page:
There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.
Upvotes: 1
Reputation: 91361
For Activity, Service, ContentProvider and Application, you should not do anything in the constructor. The first place you should do work, when you know the object is initialized and ready for use, is onCreate().
Further, please think again about "I want to start off a service when my application starts." What you code is doing here is trying to start a service when your process happens to start. I really don't think you want that. You want this service to start because you happened to receive a broadcast in the background?
If you just want to do some first time init, my recommendation is to not use Application at all. Have a singleton that can be retrieved when it is needed. Then your init happens at the point it is actually needed. There is no need for this to be associated with a service; you can just make a thread. The only reason to use a Service is to tell the system "my app is busy doing background work that the user cares about, please try not to kill me."
Upvotes: 3