android - 活动暂停时停止调用 onLocationChanged()
问题描述
我正在向服务器连续发送纬度和经度值。我在服务类中实现了 LocationListener。当在活动中调用 onPause 方法时,我需要停止向服务器发送值。我使用了以下代码但无法正常工作。stopService(new Intent(this, LocationMonitoringService.class));
谁能帮我这个 ??提前致谢
解决方案
我得到了解决方案,在片段中添加 onDestroy()。
@Override
public void onDestroy() {
super.onDestroy();
if (mLocationClient != null && mLocationClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mLocationClient, this);
}
}