posted 7 years ago
Hi Frank, Robi, and Chris!
My name is Kevin Sprague, I've been learning Android for about a month now and have a question about Android Services. I've written an application comprised of an Activity and a Service. The job of the Activity is to display updates in near real-time based on data retrieved over the Internet by the Service. The problem I'm running into is that every once in a while it would appear that the Service is being terminated by Android and so there is a delay in receiving and displaying those updates in the UI. I know that active Activities take the highest priority but in this application, the UI is not of much use if the Service isn't providing the data updates. I've thought about scrapping the Service altogether and writing the network call on a background thread in the Activity itself but that leaves me questioning the intended usage pattern for Services. Thanks in advance for your reply!
Kevin
My name is Kevin Sprague, I've been learning Android for about a month now and have a question about Android Services. I've written an application comprised of an Activity and a Service. The job of the Activity is to display updates in near real-time based on data retrieved over the Internet by the Service. The problem I'm running into is that every once in a while it would appear that the Service is being terminated by Android and so there is a delay in receiving and displaying those updates in the UI. I know that active Activities take the highest priority but in this application, the UI is not of much use if the Service isn't providing the data updates. I've thought about scrapping the Service altogether and writing the network call on a background thread in the Activity itself but that leaves me questioning the intended usage pattern for Services. Thanks in advance for your reply!
Kevin
posted 7 years ago
While I'm not sure under which circumstances Services may get terminated (and how and when they get restarted automatically), this approach will be worse - Activities can be terminated whenever Android feels like it, and they will NOT get restarted until the user does so. So it is guaranteed to fetch updates less reliably than a Service.
I've thought about scrapping the Service altogether and writing the network call on a background thread in the Activity itself but that leaves me questioning the intended usage pattern for Services.
While I'm not sure under which circumstances Services may get terminated (and how and when they get restarted automatically), this approach will be worse - Activities can be terminated whenever Android feels like it, and they will NOT get restarted until the user does so. So it is guaranteed to fetch updates less reliably than a Service.
Kevin Sprague
Greenhorn
Posts: 8
posted 7 years ago
Services should hang out much longer than an Activity, yes.
I would suggest using a model of alarms which trigger a service. The service then performs what it needs to do and then terminates. This way you control the flow and you have a ready means to "restart".
if you need to react to an event, consider a BroadcastReceiver that is triggered by some event (incoming SMS for example, or an alarm triggering).
Have a look at the AppWidget example in Android in Action, as it probably hits your need of updating the "UI" yet does so without the expense of always having an Activity plus Service running.
I would suggest using a model of alarms which trigger a service. The service then performs what it needs to do and then terminates. This way you control the flow and you have a ready means to "restart".
if you need to react to an event, consider a BroadcastReceiver that is triggered by some event (incoming SMS for example, or an alarm triggering).
Have a look at the AppWidget example in Android in Action, as it probably hits your need of updating the "UI" yet does so without the expense of always having an Activity plus Service running.
Kevin Sprague
Greenhorn
Posts: 8
Frank Ableson
Author
Greenhorn
Greenhorn
Posts: 20

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |