2

I'm fresh with Android. I face some ambitious university project, so I hope you'll tell me if my idea about following app's implementation is correct and efficient:

Business scenario:

  • WebService consumes informations about user's sms (sendt/received) and GPS/"network provided" position
  • GPS/"network provided" position's should be sendt to WebService every 1 minute (if it's value changed)
  • sms update should be sendt to WebService immediately

My implementation idea...

I'm going to use AlarmManager prepare Intent matching BroadcastReceiver (and schedule it with 1 minute interval). Then I'll start WakefullIntentService in onReceive() method. This will feed my WebService in the background. That would work for sending data over HTTP in the background.

... and doubts:

How about updating GPS/"network provided" position data in the background? Should I start some additional service and use LocationListener within it? There would be no sense for using AlarmManager then - I could feed my WebService from this location-monitoring service.

But as I read here: Diamonds Are Forever. Services Are Not. it's not a good practise to play with never-ending services. As I understand it would not work correctly when my phone is sleeping.

ncreated
  • 641
  • 1
  • 5
  • 14

2 Answers2

0

First things first, polling the GPS every minute is going to KILL your users battery. So I'd rethink about that portion of your application.

Your alarm manager idea is fine in theory with the exception of using a Wakeful IntentService. If you're waking up the phone every minute (or more often), you're once again going to kill the battery. What you should do is when you get the broadcast, save what ever data contains to disk (probably in a sqlite database). When the device fully wakes up (this is a broadcast you can recieve), then flush the content to your webservice.

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
  • First, I had to edit my post a little bit, so sorry for that move :). But it still keeps sense. What's the rational interval with GPS check out? – ncreated Oct 09 '11 at 02:12
0

Do not use the alarm manager. It is unnecessary for what you want to do.

Use a background service that implements LocationListener. You can specify how often you want to check for location updates and even how much distance must change in order to consider it a new location. This is done in the requestLocationUpdates method.

Here is some code to get you started:

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 1, this);

This will try to get a new location every 60 seconds and only if the distance has changed by 1 meter. That is the location listener will only fire if the conditions above are met.

Hope this helps.

Mike L.
  • 589
  • 6
  • 16
  • Thanks, that was helpful :). I see that I need some deep thinking about my app (fortunatelly I'm the one who creates and implements business scenario). I'll probably use LocationManager implemented in some never-ending service buffering my location data in SQLlite database as @KurtisNusbaum wrote. Then I'll feed my WebService when my phone wakes up. – ncreated Oct 09 '11 at 19:46