package roboguice.service; import java.util.HashMap; import java.util.Map; import roboguice.RoboGuice; import roboguice.context.event.OnConfigurationChangedEvent; import roboguice.context.event.OnCreateEvent; import roboguice.context.event.OnDestroyEvent; import roboguice.context.event.OnStartEvent; import roboguice.event.EventManager; import roboguice.util.RoboContext; import com.google.inject.Injector; import com.google.inject.Key; import android.app.IntentService; import android.app.Service; import android.content.Intent; import android.content.res.Configuration; /** * A {@link RoboIntentService} extends from {@link IntentService} to provide dynamic * injection of collaborators, using Google Guice.<br /> <br /> * <p/> * Your own services that usually extend from {@link IntentService} should now extend from * {@link RoboIntentService}.<br /> <br /> * <p/> * If we didn't provide what you need, you have two options : either post an issue on <a * href="http://code.google.com/p/roboguice/issues/list">the bug tracker</a>, or * implement it yourself. Have a look at the source code of this class ( * {@link RoboIntentService}), you won't have to write that much changes. And of * course, you are welcome to contribute and send your implementations to the * RoboGuice project.<br /> <br /> * <p/> * * @author Donn Felker */ public abstract class RoboIntentService extends IntentService implements RoboContext { protected EventManager eventManager; protected HashMap<Key<?>,Object> scopedObjects = new HashMap<Key<?>, Object>(); public RoboIntentService(String name) { super(name); } @Override public void onCreate() { final Injector injector = RoboGuice.getInjector(this); eventManager = injector.getInstance(EventManager.class); injector.injectMembers(this); super.onCreate(); eventManager.fire(new OnCreateEvent<Service>(this,null) ); } @Override public int onStartCommand(Intent intent, int flags, int startId) { final int startCont = super.onStartCommand(intent, flags, startId); eventManager.fire(new OnStartEvent<Service>(this) ); return startCont; } @Override public void onDestroy() { try { if(eventManager!=null) // may be null during test: http://code.google.com/p/roboguice/issues/detail?id=140 eventManager.fire(new OnDestroyEvent<Service>(this) ); } finally { try { RoboGuice.destroyInjector(this); } finally { super.onDestroy(); } } } @Override public void onConfigurationChanged(Configuration newConfig) { final Configuration currentConfig = getResources().getConfiguration(); super.onConfigurationChanged(newConfig); eventManager.fire(new OnConfigurationChangedEvent<Service>(this,currentConfig,newConfig) ); } @Override public Map<Key<?>, Object> getScopedObjectMap() { return scopedObjects; } }