0

I have tried (but in vain) to force request GPS location updates on my Android Widget (Android Oreo).

The widget is set to update every 15-20 minutes and fetch the current location of the phone and display.

However, it only picks up the location of the phone where it was installed and subsequent runs don't return a new location even when the phone moves.

I read through the solution provided here How does it work - requestLocationUpdates() + LocationRequest/Listener and implemented it, but it doesn't seem to help.

LocationManager locationManager = (LocationManager) 
context.getSystemService(LOCATION_SERVICE);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
300000L, 500.0f, new LocationListener() {

                @Override

                public void onLocationChanged(android.location.Location location) {

                    if(location != null)

                    {

                        LocationPreferences.setLastLocationLatLon(context,location);

                    }

                }



                @Override

                public void onStatusChanged(String provider, int status, Bundle extras) {



                }



                @Override

                public void onProviderEnabled(String provider) {



                }



                @Override

                public void onProviderDisabled(String provider) {



                }

            }); 

Having said that, it is more reliable when it's in one of the activities (and not in a widget), but given that the widget is supposed to perform background location updating, it doesn't work, the onlocationchanged never gets called (I'm sensing it's silently killed in the background)

Has anyone created/ worked with Android widgets using GPS on Oreo? Any help in the right direction will be appreciated. Thank you very much in advance.

Varun
  • 11
  • 2
  • https://developer.android.com/about/versions/oreo/background-location-limits.html and https://developer.android.com/about/versions/oreo/background.html would seem to be relevant. – CommonsWare Feb 15 '18 at 21:57
  • Thank you @CommonsWare. Yes, I've read through that and changed all background services. However, I am unable to find an example for getting GPS coordinates on a widget. – Varun Feb 15 '18 at 22:06
  • An `AppWidgetProvider`, on its own, cannot reliably do anything in the background. That is a manifest-registered `BroadcastReceiver`, and your process can terminate at any point in time after you update your app widget. So, you would need a `Service` for any version of Android, if you want reliable results. It's just now that it will need to be a foreground service, both so the service is not destroyed after a minute in the background and so you are hopefully "foreground enough" to get location updates. – CommonsWare Feb 15 '18 at 22:33
  • Thank you @CommonsWare. I take it that foreground services work with widgets then? Couldn't find it on google's documentation. – Varun Feb 16 '18 at 23:29
  • App widgets do not care how you update them. If you want to use a foreground service, go right ahead. – CommonsWare Feb 16 '18 at 23:42
  • Thank you @CommonsWare you saved my day mate – Varun Feb 22 '18 at 22:10

0 Answers0