onStartCommand after service process is killed when started with START_STICKY

后端 未结 4 1727
天命终不由人
天命终不由人 2021-02-13 06:15

I have been reading the Android documentation and I am wondering if anyone can shed some light on what happens to a service instance when a service started with START_STICKY has

4条回答
  •  情话喂你
    2021-02-13 06:48

    I recently came across this same problem. Service provides no built in means of saving state and the last intent may not be enough to get the service back to its previous state. My solution was to have the activity persist state and pass that state to the service via startService(). The service then just fires events at the activity, like:

    • here's an update
    • something died and here's the exception
    • I've been killed, please restart me with any necessary state

    This approach cleaned up my design a lot, and both service and activity are resilient to being killed.

提交回复
热议问题