Application threads vs Service threads

后端 未结 3 1419
余生分开走
余生分开走 2020-12-01 10:30

What are the advantages/disadvantages in placing a lengthy network access code in a thread in an activity or a thread in a service? How would it affect the application? I

3条回答
  •  情书的邮戳
    2020-12-01 10:56

    From my experience (1+ years developing Android), there is no difference between running a new thread in a service or in an activity. Try not to keep a reference to the Activity in the new thread - use the application context.

    Also, the service's life-cycle didn't help at all because some methods are not guaranteed to be invoked :(

    The only difference may be that the service can be destroyed without destroying the app completely - thus potentially destroying the new threads. Why potentially? because on the practical side, this doesn't happen. The app ALWAYS gets killed without killing the service before that, meaning: the local service mechanism is useless!!!

    Remote service is a different discussion - I was referring only to "where should I run a new thread?".

    Good luck!!!

提交回复
热议问题