Service and IntentService, Which is better to run a service that poll database value from the server?

一笑奈何 提交于 2019-12-06 08:31:18

Intent Service -

  • Works in Worker Thread , not in Main Thread.

  • Intended to execute their action is separate thread and then get shut down.

  • They do perform their operation and stops.

  • Ideal to perfrom things like htp get ,don't require to stay connected with server.

Service -

  • Runs in main thread.

  • Ideal when there is requirement to stay connected with server (i.e. permanent tcp connection), the way you can go is to have a service (not an intent one) that performs the networking stuff using an asynctask or a more classic thread hosted in the service

It makes no difference. Use whatever you find easier. This question isn't worth spending any time worrying about. Just make sure you understand what code needs to run on the main (UI) thread and what code needs to run on a background (worker) thread. In IntentService the "long-running operation" needs to run in onHandleIntent() If you are using Service in onStartCommand() you would start your own background thread and execute the "long-running operation" on that.

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