IntentService - handy but missing a constructor
2010-07-11 06:16:50
The IntentService class is handy indeed. It's a service that spawns a separate thread for the work it does. All you need to do to use it is to extend it, override the onHandleIntent method, and then call startService to invoke it. E.g. something like this:
public class MyIntentService extends IntentService {
protected void onHandleIntent(Intent intent) {
// do some work
}
}
public class MyActivity extends Activity {
...
void startMyService() {
Intent i = new Intent(this, MyActivity.class);
// The work will be done in a separate thread, so it doesn't block
// this activity
startService(i);
}
}
At least that's what the current doc about IntentService says. There's one more important thing to know though: the MyIntentService has to a default constructor, that takes no arguments, e.g. as so:
MyIntentService() {
super("MyIntentServiceName");
}
The system expects there to be such a constructor, and if it is missing you will get a InstantiationException when startService is called (if there's no constructor at all you will get a compilation error).
The string passed to super above is arbitrary, and I can't really say I know what it is used for. It is needed since the only constructor exposed by IntentService takes a String argument.
Enjoy using your IntentServices.
Posted by android 2
0 CommentsPage 1