I do not quite understand some aspects of the implementation of Service in the Android OS:

  1. I start the Service , in which there is a field private int depCount; . In the process of Service it changes. I stop the service using the stopService() method. When I restart it, the depCount field depCount no longer null - it takes on the value that was before the previous service stop. Why it happens?
  2. If I call the startService() method for one Service several times in a row, it will only be run once, right?

    1 answer 1

    1. Take a look at the lesson there is a good talk about Service . The variable depCount shows how to behave if the Service killed.
    2. Yes, it will only be started once, until the Service is stopped or killed.
    • The second is clear, thank you. But with the first one I did not understand .. why the depCount variable is initialized to zero only when the service is started for the first time, and in subsequent ones it somehow takes the previous values. - minGO
    • Because he has never been stopped. It is necessary to alternate when the service is started / stopped. Services work in the background, i.e. regardless of the activity (you can exit the application and the service will still work). At the start of the activity that starts the service, the service looks at the value of the variable and determines the current state. - Alexey Shtanko
    • No, I stop it, then I launch it again ... but just what I understood is that it is not a service, the variable is initialized as it should. The point is on the onLocationChanged method. The service is used to update the coordinates and the variable depCount is used in the onLocationChanged method. There it changes, and the next time it is started, it is initialized by zero again, but in the onLocationChanged method each time the values ​​will be taken from the previous time .. I don’t know why I’ll understand. Thank. - minGO