6

According to this blog post and the documentation of onStartCommand() if you have a Service you should implement onStart() and onStartCommand() and in 2.0 and higher only onStartCommand() will be called. It seems that this is not the case and in my Service BOTH are being called. This was a problem as it was trying to do the work twice, so I had to add a check in onStart() to not do anything if the OS version was < 2.0. This seems like a hack and a bug. Anyone else experience this or do I maybe have something wrong? I cut and pasted the code right from the sample.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
  Util.log(mCtx, "AlerterService", "onStartCommand() called");
  handleStart(intent);
  return super.onStartCommand(intent, flags, startId);
}

public void onStart(Intent intent, int startId) {
    Util.log(mCtx, "AlerterService", "onStart() called");       
    handleStart(intent);
    super.onStart(intent, startId);
}
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
sroorda
  • 63
  • 1
  • 1
  • 4

2 Answers2

12

The source code of onStartCommand() is:

    public int onStartCommand(Intent intent, int flags, int startId) {
        onStart(intent, startId);
        return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;
    }

So it still calls onStart();

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
songzhw
  • 272
  • 3
  • 11
3

On that blog post, the base implementantions of onStart and onStartCommand are not called. Pressumably, one of them is calling the other.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • Nice catch, totally missed that. You were right on. Once I got rid of the super() calls only the proper methods were called. – sroorda Sep 21 '11 at 00:34