开发者

Android services: life cycle considerations

开发者 https://www.devze.com 2023-03-25 03:40 出处:网络
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.

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 litt开发者_StackOverflow中文版le bit overwhelmed with all this!

Thanks a lot in advance everybody!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号