• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What Am I Missing About Android Services?

 
Kevin Sprague
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that makes sense. Do you know if there is a way to make the priority of a service at least equal to that of an Activity?
 
Frank Ableson
Author
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kevin Sprague
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Frank, thanks for the additional information. If I'm understanding correctly, the BroadcastReceiver will automagically fire up when a matching Intent is triggered by some event. If this is the case then this is truly asynchronous and an elegant solution. Thanks!
 
Frank Ableson
Author
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes asynch! Just be sure to properly wire up your androidmanifest.xml file!
 
Kevin Sprague
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LOL, yes I've made that rookie mistake more than I'd like to admit.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic