-3

I have a background thread that needs to get the GPS location every 1 hour. What would be the best way to do this?

  1. locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000*60*60,0, this);

  2. have the thread sleep for a hour and then register for updates and after receiving the location will

    removeUpdates(...)

And what is the difference between LocationManager.NETWORK_PROVIDER and LocationManager.GPS_PROVIDER.

Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
ilan
  • 4,402
  • 6
  • 40
  • 76
  • [NETWORK_PROVIDER](http://developer.android.com/reference/android/location/LocationManager.html#NETWORK_PROVIDER) provides the Name of the network location provider. and [GPS_PROVIDER](http://developer.android.com/reference/android/location/LocationManager.html#GPS_PROVIDER) provides the Name of the GPS location provider. –  Mar 24 '15 at 06:41
  • did you tried something? –  Mar 24 '15 at 06:41

3 Answers3

0

Instead of using LocationManager use Location API . It provides much better accuracy and eats less power and its faster at getting the location.

j4rey
  • 2,582
  • 20
  • 34
  • that requires play services, which may not be available on a given device. – njzk2 Nov 05 '14 at 22:40
  • So? check if its available, use location api,which is a better way of getting gps co-or, if not, use locationmanager. Either way Google Play Services is automatically installed on any Android device that runs Android 2.2 or higher. And i have used LocationManager, takes 2-5 mins to get a lock on, sometimes more, and its aint that accurate.....Using LocationClient i get a lock on in milliseconds. [watch Google I/O](https://www.youtube.com/watch?v=Bte_GHuxUGc) – j4rey Nov 06 '14 at 18:35
  • `Google Play Services is automatically installed on any Android device that runs Android 2.2 or higher.` don't think so. you need Google Play Store to have play services. – njzk2 Nov 06 '14 at 18:40
  • Play eats less power, but for a special application that needs only one location per hour, your answer is not helpfull. It's more a general hint. probably it should be a comment. – AlexWien Nov 06 '14 at 19:21
0

Your second option from point of GPS consumption is the best one. But instead letting the thread to sleep for an hour I would use a timer with period = 1 hour. If you cannot garuantee that your app will stay active for that hour, you want to store the last reveived location time. Once your app wakes up next time you check the system time stamp and compare it to your last location time on permanent storage. If the hour was exceeded you immeadetly demand a location, and later continue with timers. If the hour has not yet passed, the you calculate the time difference and start a timer with initial delay of that difference, and period of 1hour. But thi sanswr is not dealing how to keep your app alive, it tells you from point of power consumption that is is better to enable GPS only for that short moment you need it.

Getting a GPS location will need from 20-40s.
You should set highest accuracy. (GPS) After enabling GPS and receiving a valid location, check the horicontal accuracy.
Leave GPS enabled until the accuracy reach the desired threshold (e.g <15m). Then disable GPS.

With that solution GPS needs only power for about a minute every hour, which is negligible low.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
  • `have a thread sleep for an hour` is the best solution in your opinion? – njzk2 Nov 05 '14 at 22:41
  • @njzk2 a timer would be more appropriate, I updated the answer. But this is a minor detail. – AlexWien Nov 06 '14 at 19:18
  • no. a timer would not be more appropriate. This is android. your process, unless being a foreground process, will never last 1 hour. – njzk2 Nov 06 '14 at 19:34
  • @njzk2 That depens what else the app is doing. If the app accesses the network every 10 seconds, or do some other activity that prevents it from beeing sent to sleep. Butt this is another question. The question was about geting location once an hour with low battery consumption. The question was not how to kepp the App active. Further he is talking about a second (background) thread which handles the location. Its the job of his other app threads (main) to prevent shutdown of the whole app. – AlexWien Nov 06 '14 at 20:26
  • yes, exactly, once an hour. If you use a timer with a 1 hour interval, in order for your timer to work, the app needs to stay active for all that time. Otherwise, with the destruction of the process, the timer is removed as well. – njzk2 Nov 06 '14 at 20:27
  • Updated again, its not job of this answer to give a working program, with all posisbilities that his app would be terminated. It deals the GPS task, and how to get approriate power consumption for a location once a hour. – AlexWien Nov 06 '14 at 20:38
-1

You can use Criteria for that, customize it according to your need.

A class indicating the application criteria for selecting a location provider. Providers maybe ordered according to accuracy, power usage, ability to report altitude, speed, and bearing, and monetary cost.

code sample :

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);

Have a look at how to use locationmanager , how to specify Criteria and how getBestProvider method works for reference

Maveňツ
  • 1
  • 12
  • 50
  • 89