Android 我无法从片段中停止位置服务。怎么能阻止它?

Android 我无法从片段中停止位置服务。怎么能阻止它?,android,service,android-service,android-ondestroy,Android,Service,Android Service,Android Ondestroy,我从onCreateView开始我的服务,就像 getActivity().startService(new Intent(getActivity(), LocationService.class)); 我试图阻止我这样的服务 @Override public void onDestroyView() { getActivity().stopService(new Intent(getActivity(),LocationService.class)); super.o

我从onCreateView开始我的服务,就像

getActivity().startService(new Intent(getActivity(), LocationService.class));
我试图阻止我这样的服务

    @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();
}

调用stopService应该就足够了,即使在onDestroy中调用一次也可以。确保您在服务中实际实现了onDestroy,并在其中注销了LocationListener。

请尝试onStop中的locationmanager方法或onDestroy服务方法


现在你可以使用googleplaces进行定位,它提供了简单的实现。请参阅这里:-

我在我的服务类中没有使用LocationManager,但我使用了GoogleAppClient,经过多次尝试,我在onDestroy中使用了以下代码。最后,它完成了

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