Android_guides: Not getting location updates when gps is turned off

Created on 27 Apr 2020  ·  5Comments  ·  Source: codepath/android_guides

In a foreground service, i'm using FusedLocationProviderClient to request for location updates. I'm using foreground service because, i want location updates even when the app is closed. so far so good. it is working. But the problem is, if the gps is turned off then i'm not getting the onLocationResult() call back. How to handle this problem ?? Before starting the service, location was on and i got a couple of callbacks in onLocationResult() method. After sometime, user turned off the gps, after that i'm not getting the onLocationResult() call back. Any idea how to deal with this problem ??

All 5 comments

There 2 possible approach I may recommend:

  1. Check or change your Location request priority settings to use
    PRIORITY_BALANCED_POWER_ACCURACY(uses any available service) instead of PRIORITY_HIGH_ACCURACY that replies mostly on GPS according to Google.
    E.g.
    LocationRequest locReq = LocationRequest.create(); locReq.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
  1. Implement a Broadcast Receiver for changes to gps state and make a new location request using another Location Service Provider e.g. Network or Wifi if the GPS is off

There 2 possible approach I may recommend:

  1. Check or change your Location request priority settings to use
    PRIORITY_BALANCED_POWER_ACCURACY(uses any available service) instead of PRIORITY_HIGH_ACCURACY that replies mostly on GPS according to Google.
    E.g.
    LocationRequest locReq = LocationRequest.create(); locReq.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
  2. Implement a Broadcast Receiver for changes to gps state and make a new location request using another Location Service Provider e.g. Network or Wifi if the GPS is off

I already implemented both of these approaches. In the second approach, instead of starting a new location request, i'm just raising a audio notification to the user to switch on the GPS. If i make a new location request using another location service provider, will it automatically turn on the GPS or will it prompt a message to the user to TURN ON GPS ??

Other location providers shouldn't prompt user to turn on the GPS and it won't turn the GPS on automatically, runtime permission is required.
It depends on your requirements;

  • If it is like a tracker then notifying user to turn on the gps is best options
  • If you don't need precise use location i.e. you only need city-wide location then WiFi or Network providers should do, caveat is less accurate.
  • Other option is to call 3rd party location api if the gps is off and you still need location updates.

Other location providers shouldn't prompt user to turn on the GPS and it won't turn the GPS on automatically, runtime permission is required.
It depends on your requirements;

  • If it is like a tracker then notifying user to turn on the gps is best options
  • If you don't need precise use location i.e. you only need city-wide location then WiFi or Network providers should do, caveat is less accurate.
  • Other option is to call 3rd party location api if the gps is off and you still need location updates.

Yes, it is a tracker. So I'm notifying the user to turn on the GPS. Can you give more details about the last option, calling 3rd party location APIs. What 3rd party location APIs are available and what have you used ?

There are lots of options but I have had a quick search for you to kick start your research 😉.
See below for the links.
I tried openweathermap.org api before and was alright.
Most of the 3rd-party location api uses user device's ip address to do the magic so it might not be accurate as well.
https://ipfind.com/
https://www.google.com/amp/s/rapidapi.com/blog/ip-geolocation-api/amp/

Advice:

  1. When the GPS is off, save the last user location locally, preferably use Shared Preference with a relative key e.g. Gps_LastLocation
  2. Compare the newly pulled location from any other providers aside GPS, with last saved location.
  3. If same happy days if not notify user to confirm the new location. It is a lot of work but that's what I will do as a fall back.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

savekirk picture savekirk  ·  8Comments

betorobson picture betorobson  ·  71Comments

debpedrano picture debpedrano  ·  8Comments

nesquena picture nesquena  ·  4Comments

nesquena picture nesquena  ·  11Comments