2

I have been working on a Location-based app, and everything is ok except for the location implementation, which is the core point.

I know location is very expensive, but I couldn't handle this by taking into account battery consumption.

Here is my current algorithm:

Step 1: Create an alarm manager with time interval of 1 minute.

Step 2: When alarm is triggered, start the location listener

if(settings.getBoolean("use_gps", false))
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
    manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,listener);
    manager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0,listener);

Step 3: Wait a max of 15 seconds for a location

Step 4: When a location is received, stop the location listener..

One of the problems is due to this: at 0 seconds,with 0 meters location accuracy, CPU usage increases tremendously -- 45% CPU when alarm is triggered, with 0-15 seconds of alarm.

But when I look at battery usage of my app, the battery usage increase, after 1-2 hours, does not match the expectation: battery usage is only ~ 4% - 5%.

The second problem comes when the user wants to get their location by GPS. Because getting a location by GPS takes so long, compared with getting location from the network provider, I couldn't get the location by GPS.

I have implemented this algorithm after implementing a lot of different methods.

I am looking for recommendation from someone who has successfully implemented periodic location retrieval via GPS with low CPU usage.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
barisatbas
  • 570
  • 2
  • 9
  • 22

2 Answers2

2

Use receiver and service together. For this purpose you can find a complete sample in this link. There is a listener in it. Listener can be used in your activity to be noticed that a new location is ready for you. Also there is a service with an AlarmManager in it.

Community
  • 1
  • 1
Bob
  • 22,810
  • 38
  • 143
  • 225
1

Check out Little Fluffy location library it does all the location 'heavy' lifting and follows Google's location pro tips -> https://code.google.com/p/little-fluffy-location-library/

scottyab
  • 23,621
  • 16
  • 94
  • 105