-2

I start my service from onCreateView like

getActivity().startService(new Intent(getActivity(), LocationService.class));

And I try to stop my service like this

    @Override
public void onDestroyView() {
    getActivity().stopService(new Intent(getActivity(),LocationService.class));
    super.onDestroyView();
}

@Override
public void onStop() {
    getActivity().stopService(new Intent(getActivity(), LocationService.class));
    super.onStop();
}

@Override
public void onDestroy() {
    getActivity().stopService(new Intent(getActivity(), LocationService.class));
    super.onDestroy();
}
Harsh Patel
  • 657
  • 11
  • 26

3 Answers3

0

Calling stopService should suffice - even once in onDestroy. Make sure you actually implement onDestroy in your service and unregister your LocationListener inside it.

marcinj
  • 48,511
  • 9
  • 79
  • 100
0

try this method of locationmanager in onStop or ondestroy method of servise

locationManager.removeUpdates(LocationTracker.this);

These day u can use googleplaces for location it provides simple implementation Refer here:- http://coderzpassion.com/android-location-using-google-play-services/

Jagjit Singh
  • 1,909
  • 1
  • 14
  • 19
0

I didn't used LocationManager in my service class but I used GoogleApiClient and after a lot try I used following code in onDestroy(). Finally, it is done.

@Override
public void onDestroy() {
    super.onDestroy();
    if (googleApiClient != null && googleApiClient.isConnected()) {
        LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
    }
}
Harsh Patel
  • 657
  • 11
  • 26