yayaa / LocationManager

Simplify getting user's location for Android
806 stars 187 forks source link

Getting location in intervals in Service #42

Closed SuperMasterBlasterLaser closed 7 years ago

SuperMasterBlasterLaser commented 7 years ago

I have created service that extends from your LocationBaseService And in getLocationConfiguration I have set this:

@Override
public LocationConfiguration getLocationConfiguration() {
    return new LocationConfiguration.Builder()
            .keepTracking(true)
            .useGooglePlayServices(new GooglePlayServicesConfiguration.Builder().askForSettingsApi(false).build())
            .useDefaultProviders(new DefaultProviderConfiguration.Builder()

                    .requiredTimeInterval(5000).build())
            .build();
}

I haven;t changed onStartCommand and method onLocationChanged is not calling.

Do I need to call getLocation method explicitly every time?

yayaa commented 7 years ago

Do you already have your permissions granted? Otherwise, in service, it'll fail immediately.

Besides that you wouldn't need to call getLocation everytime, calling once when your configuration asks for keeping track should be fine, and you should receive location changes whenever it changes :)

P.S.: Please also share your log output, so that i can see what exactly happening.

SuperMasterBlasterLaser commented 7 years ago

@yayaa I have my permissions granted. When I make a call getLocation explicitly in onStartCommand it shows my my location.

However, when I start walking aroung with my GPS turned on, I do not get any updated new locations.

My logs shows this (when I call explicitly):

4-18 15:57:26.498 1268-1268/my.app I/GooglePlayServicesLocationProvider: GoogleApiClient is connected.
04-18 15:57:26.508 1268-1268/my.app I/GooglePlayServicesLocationProvider: LastKnowLocation is available.
04-18 15:57:26.518 1268-1268/my.app I/GooglePlayServicesLocationProvider: Ask for location update...
04-18 15:57:26.518 1268-1268/my.app I/GooglePlayServicesLocationProvider: SettingsApi is not enabled, requesting for location update...
04-18 15:57:28.248 1268-1268/my.app D/LOCATION: Location[fused 43,230198,76,944996 acc=20 et=+12d19h8m15s212ms]
yayaa commented 7 years ago

One more thing, how is your service configured? Does it survive after receiving location? What are you returning onStartCommand?

Be aware system might kill your service, and after that of course you'll not be receiving any location update. See also the sample implementation of service calls stopSelf method. (Make sure you don't call it unless you need location updates)

SuperMasterBlasterLaser commented 7 years ago

Yes. I don't use stopSelf() method. In my onStartCommand I return START_NOT_STICKY. But when I place this code in my onStartCommand:

new Timer().schedule(new TimerTask() {
        @Override
        public void run() {
            getLocation();
        }
    }, 0, INTERVAL);

My onLocationChanged returns location but always the same.

yayaa commented 7 years ago

Do not call getLocation method in timer, it handles itself in the library. You just need to call once.

And please make sure your service survives. You can try to return START_STICKY just to test, this is not the best practice, tho.