0

i am currently trying to get code running as a background service. what this code does is:

  • send request to server with current location of the user
  • receive response
  • parse response
  • save into model (singleton)

and this is set to happen in a 30 sec interval again and again.

now if my app stays in the background for too long, it will get disposed by the device and that code will not be executed anymore. what would be the right kind of background service for this usecase?

one of my main concernes is that i save my data in a singleton. but if my app is disposed this singleton will probably not exist anymore.

intent service doesnt make sense imho because it runs a one time tasks and has to be restarted from an app that might already be disposed at that point. using the alarm manager would mean that i will have to save everything out of the app (sqllite for example) and then retrieve that data when the activity is started again which sounds rather complicated.

can someone please help me out here?

thanks in advance!

stephanlindauer
  • 1,910
  • 2
  • 14
  • 17
  • what about an Broadcast Receiver who starts a service when data has arrived? – Ilja KO Aug 05 '15 at 00:29
  • but from where would the broadcast be triggered if the activity doesnt exist anymore? – stephanlindauer Aug 05 '15 at 19:15
  • I don't know exactly but I have always in mind that you can specify what the receiver listens to and so you could listen when data arrives and this data should make an broadcast through the system so that the receiver can receive it...but sry I don't know how :p – Ilja KO Aug 06 '15 at 04:13
  • But as I suggested the hack with 2 services controlling each other could do the trick ;) – Ilja KO Aug 06 '15 at 04:15

1 Answers1

1

You sir needs the service of GCM https://developers.google.com/cloud-messaging/

Thats exactly what you need for your desire ;)

But it's not less complicated as sticking to background services.

Also you can do a Hack: having two services watching your service to keep on running and itself...I swear when the User doesn't stop your app manually in the menu the System won't be able to stop them itself. Foolproof.

Ilja KO
  • 1,272
  • 12
  • 26
  • but in my case i want to ask a webserver for updates and then save that data. i dont want to rely on messages being passed through a cloud to my device. this definitely is more battery consuming but there's no way around it because i need to send the devices location to the server anyway when i make the request. – stephanlindauer Aug 05 '15 at 11:28
  • Why just not use a long running service? – Ilja KO Aug 06 '15 at 04:17
  • What kind of data is it only normal variables? Like number and so on? You can use shared preferences for that.not so complicated like database or files – Ilja KO Aug 06 '15 at 04:19
  • just to make sure: by "long running service" you mean an IntentService? – stephanlindauer Aug 06 '15 at 20:20
  • the data could be represented as a json string. i could save that string into sharedprefs. so lets say saving to shared prefs and parsing and rendering that data when the activity comes back on works. the problem continues to be that i cant be sure the service wont be killed. – stephanlindauer Aug 06 '15 at 20:20
  • You never can be sure thats right....so why dont you want to use the alarm manager to check if service is running and when not restarting it again? – Ilja KO Aug 07 '15 at 00:39
  • 1
    also read my hack tip with the two services controlling each other ;) – Ilja KO Aug 07 '15 at 00:39
  • do you have a link or something to point me into the right direction? cant find anything googling it... – stephanlindauer Aug 11 '15 at 19:39