Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android services: life cycle considerations

I am making an android app which will have two services that will keep sending data about the usage of the phone by the user every 24 hours.

The user should execute the app, toggle the buttons to enable the logging of the usage of the phone and then the user should be able to do a normal life with his phone, until he starts again the app and disables the toggle button to stop the logging of the info.

  • What considerations should I take about the life cycle of the services?
  • What about the interaction of the user with the phone while the services should be sending the data?

All info is very much appreciated, as I my mind is getting a little bit overwhelmed with all this!

Thanks a lot in advance everybody!

like image 769
noloman Avatar asked Aug 03 '11 12:08

noloman


1 Answers

The service can be cut at any time through the settings menu. It can also be killed at any time by Android if it decides it needs the resources for the currently running activity. onDestroy() will be called regardless so use that to store anything needed.

The service runs in the background but through the main UI thread. Thus, it is possible to block operation of the phone through a service. It looks like the phone locked up when it's really a service trying to do something. Any blocking procedure should be used in a thread such as Java timer, Java thread, or AsyncTask.

There can only be one running version of the service at any given time. However, calling startService(myService) if "myService" is already running will essentially override the current running service and onStartCommand() will be called again. However, one call to stopService(myService) is needed to stop it no matter how many times startService(myService) was called.

stopService(myService) will not stop a service if the service is bound to anything. It will wait until all bindings are removed before the service stops.

like image 93
DeeV Avatar answered Sep 18 '22 13:09

DeeV