Sunday, August 11, 2013

Custom IntentService

In your AndroidManifest.xml file, you must declare the service using open and close tags. A single tag with a close slash does not work.

<service android:name=".subpackage.MyService"></service>

The <service> must be a direct child of the <application> 

You need to override onCreate():

public void onCreate() {
   super.onCreate(); 


The constructor of your custom IntentService must not take a parameter. It should just call super("anything") with a string parameter that is used for debugging only.

public MyService() { 
   super("MyService"); 


It also needs to override the onStartCommand method:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) {
   super.onStartCommand(intent, startId, startId); 
   return START_STICKY; 
}