what to return in onStartCommand for a service

2020-06-08 19:40发布

I have been looking through the documentation and sometimes the onStartCommand() returns START_NOT_STICKY, sometimes it returns the following:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    return super.onStartCommand(intent, flags, startId);
}

I am now confused as to why some services return super.onStartCommand(intent, flags, startId);

标签: java android
3条回答
虎瘦雄心在
2楼-- · 2020-06-08 20:24

It all depends on what you want. The documentation says:

For backwards compatibility, the default implementation calls onStart(Intent, int) and returns either START_STICKY or START_STICKY_COMPATIBILITY.

So returning super.onStartCommand() is equivalent to returning START_STICKY. If you don't want the default behavior you can return another constant.

查看更多
一夜七次
3楼-- · 2020-06-08 20:25
 Service.START_STICKY
>> the system restarts the service with everything refresh not using the previous intent. 
Service.START_NOT_STICKY 
>> the system does not restarts the service.
Service.START_REDELIVER_INTENT
>>the system restarts the service with using the previous intent.`enter code here
查看更多
Animai°情兽
4楼-- · 2020-06-08 20:37

The most often used are

  • Service.START_STICKY
  • Service.START_NOT_STICKY and
  • Service.START_REDELIVER_INTENT

Service.START_STICKY will restart if the android system terminates for any reason. Service.START_NOT_STICKY will run till it has pending works. Service.START_REDELIVER_INTENT is similar to Service.START_STICKY but the original Intent is re-delivered to the onStartCommand method.

查看更多
登录 后发表回答