Local service or remote service?

混江龙づ霸主 提交于 2019-12-03 14:46:46

问题


I have a background Service which must run permanently. The service just has to interact with my Activity.

  1. How to i check on activity resume if the service is still running? Is there a better possibility than a static variable in the service class?

  2. Is it of advantage to use a remote service in a separate process(to extend service life time) so that when the activity process gets killed the service is still alive?


回答1:


I have a background service which must run permanently.

This is not possible. The user or Android will kill off your service at some point. Please reconsider your architecture.

How to i check on activity resume if the service is still running?

Generally, you don't.

Is there a better possibility than a static variable in the service class?

That will not work if the user or Android kills off your service.

Is it of advantage to use a remote service in a separate process(to extend service life time) so that when the activity process gets killed the service is still alive?

A remote service has nothing to do with whether the service runs after activities are destroyed. If you call startService(), the service will run, independent of any activities, until:

  • you call stopService() from an activity
  • the service calls stopSelf()
  • Android terminates the service
  • the user terminates the service via the Settings application
  • the user terminates the service via a "task killer" (Android 2.1 and earlier, at least)



回答2:


Why do you want to know if the Service is running? If you need something from it, just throw an Intent and if it's not running it will start by the intent.

In relation with the second question:

Your service will not "die" when your Activity closes.




回答3:


Start the service in startforeground(). It will increase the timespan of the service.



来源:https://stackoverflow.com/questions/3281890/local-service-or-remote-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!