Four components of (b) service - Service

Use  IntentService  significantly simplifies the implementation to start the service. However, if the service requires the implementation of multi-threaded (rather than through the work queue handler startup request), you can extend  Service  class to handle each of Intent. For each request start, it was the use of worker threads to execute the job, and each handle only one request.

5777390-cfc6e83075bd504f.png


5777390-55acc66fa15be9e9.png

As you can see, the use of  IntentService  compared, which requires more work.

However, because is handled by your own  onStartCommand ()  for each call, so you can execute multiple requests at the same time. This example does not do so, but if you so wish, you can create a new thread for each request, and then run these threads immediately (rather than waiting for a request to complete) is.

Please note, onStartCommand ()  method must return integer. Is an integer value used to describe how the system should continue to run the service in the case of termination of service (as described above, IntentService  default implementation will handle your case, but you can modify it). From onStartCommand ()  Returns the value must be one of the following constants:

START_NOT_STICKY

If the system  () onStartCommand  after returning termination of service, unless there are pending Intent to be passed, otherwise the system will not re-establish service. This is the safest option when running the service can avoid unnecessary applications can easily reset all unfinished work and.

START_STICKY

If the system  () onStartCommand  after termination of service return, and will rebuild service call  onStartCommand () , but will not be redelivered last Intent. On the contrary, unless there are pending Intent To start the service (in this case, pass them Intent), otherwise the system will call by empty Intent  onStartCommand () . This applies to not execute the command, but run indefinitely and wait for the media player a job (or similar services).

START_REDELIVER_INTENT

If the system  () onStartCommand  after the termination of the return service will re-establish service, and passed to the service through the last Intent calls onStartCommand () . Any pending Intent were passed in turn. This applies to active service job is executed (such as downloading files) should be immediately restored.

For more detailed information about these return values, see the reference documentation for each constant link.

Baidu network disk  extraction code: 575c

Reproduced in: https: //www.jianshu.com/p/f703c5b1bfa9

Guess you like

Origin blog.csdn.net/weixin_33738555/article/details/91244160