onStart() and onStartCommand() still called in 2.0 and higher

别说谁变了你拦得住时间么 提交于 2019-12-01 02:51:23

问题


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);
}

回答1:


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




回答2:


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();



来源:https://stackoverflow.com/questions/7493247/onstart-and-onstartcommand-still-called-in-2-0-and-higher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!