4

I'm developing an application which gets the user location at start. I am using Smart Location Library to fetch the location and reverse geocode. But the main problem is that GPS is connected showing in notification even after application closed. I checked the stack trace but did not found any leaked window exception there. I'm using this code to fetch my location..

private void getMyLocation(){
    final long mLocTrackingInterval = 1000000 * 5; // 5 sec
    float trackingDistance = 1000 * 10;
    LocationAccuracy trackingAccuracy = LocationAccuracy.HIGH;
    LocationParams.Builder builder = new LocationParams.Builder()
                .setAccuracy(trackingAccuracy)
                .setDistance(trackingDistance)
                .setInterval(mLocTrackingInterval);
    SmartLocation.with(mContext)
                .location(provider)
                // .continuous()
                .oneFix()
                .config(builder.build())
                .start(this);
}

@Override
public void onLocationUpdated(Location location) {
    SmartLocation.with(mContext).geocoding()
            .reverse(location,new OnReverseGeocodingListener() {
                @Override
                public void onAddressResolved(Location location, List<Address> results) {
                    if (results.size() > 0) {
                        mAddressList=results;
                        mLocation=location;
                        tvLocation.setText( results.get(0).getAddressLine(0)+"\n"+results.get(0).getAddressLine(1));
                    }
                }
            });
}

And onStop() method of the Main actvity..

@Override
protected void onStop() {
    super.onStop();
    SmartLocation.with(mContext).location(provider).stop();
    SmartLocation.with(mContext).geocoding().stop();
}

Edit

I'm using this povider. I've tried other providers too. But still the same result.

private LocationGooglePlayServicesWithFallbackProvider provider=new LocationGooglePlayServicesWithFallbackProvider(mContext);

I've tried a lot but am not able to figure out what is the actual problem. Any help would be Appreciated. Thanks

Tadija Bagarić
  • 2,495
  • 2
  • 31
  • 48
Abhishek Singh
  • 9,008
  • 5
  • 28
  • 53

1 Answers1

3

For an App to stay responsive a lot of things are done Async. This is a good thing. It looks like the Library you use works that way. See the Sample of MainActivity.java. onActivityResult() is the one you are looking for.

So, when does it come back with data? We just don't know for sure...

How do you stop it then? Well, that is an other part of Android. Please see this Android Activity Lifecycle. You will notice that while onStop() is usually called, it is not the first thing when the user does other stuff. If you wait for onStop() to be called, you don't know when that is. If a user moves away from an App and manually deletes it from memory, only onPause() is called for sure, the rest in my experience is a bit of hit and miss.

So if you want to kill the GPS when the user moves away (but your App is still in the memory, ready to receive notices) you do that in onPause(). Something like this:

@Override
protected void onPause() {
    super.onPause();

    if (SmartLocation == still_running) { // pseudo code
        SmartLocation.with(mContext).location(provider).stop();
        SmartLocation.with(mContext).geocoding().stop();
    }
}
  • So, basically, you claim that `onStop()` is **not** guaranteed to be called, whereas `onPause()` is, which is not true. `onStop()` will be called even in the case when `user moves away from an App and manually deletes it from memory`. – azizbekian Apr 24 '17 at 14:06
  • That is indeed my experience. I know the Docs say it is called every time. But it created an error in an App of mine due to being called, if at all, at and other time then expected. – Flummox - don't be evil SE Apr 24 '17 at 14:10
  • Can you post a simple project demonstrating that behaviour? I cannot reproduce that. – azizbekian Apr 24 '17 at 14:16
  • For now, no. As I don't think it will help with the Original question. (as that would be a different question, and I have it working the way I want) Do you have an (other) idea where it might go wrong with this Question? – Flummox - don't be evil SE Apr 24 '17 at 14:21
  • 1
    If I had a precise answer, which I could clarify, I would post it. As long as I do not have one, I see how others do that. – azizbekian Apr 24 '17 at 14:24
  • I don't know why `onStop()` not called everytime. It should be called according to Developer Docs – Abhishek Singh Apr 27 '17 at 03:17
  • @Abhishek Singh, what made it work for you? Moving to the onPause()? – Flummox - don't be evil SE Apr 27 '17 at 12:16
  • yes @Flummox. `onActivityResult` has no use cause its for `LocationGooglePlayServicesProvider` and i'm using `LocationGooglePlayServicesWithFallbackProvider` – Abhishek Singh Apr 27 '17 at 12:18