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

I cannot stop Location service from my fragment. How can stop it?

我从 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或ondestroy服务方法中尝试locationmanager的这种方法

locationManager.removeUpdates(LocationTracker.this);

现在你可以使用 googleplaces 定位它提供了简单的实现参考这里:- http://coderzpassion.com/android-location-using-google-play-services/

我没有在我的服务中使用 LocationManager class 但我使用了 GoogleApiClient 并且经过多次尝试后我在 onDestroy() 中使用了以下代码。终于搞定了。

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