在onDestroy中
locationManager.removeUpdates(locationListener);
locationListener = null;
Run Code Online (Sandbox Code Playgroud)
匿名实现
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null) {
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (chatId != null) {
double radius = 6378.137;
double oldLat = mLastLocation.getLatitude();
double oldLng = mLastLocation.getLongitude();
double newLat = location.getLatitude();
double newLng = location.getLongitude();
double dLat = Math.toRadians(newLat - oldLat);
double dLng = Math.toRadians(newLng - oldLng);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(oldLat))
* Math.cos(Math.toRadians(newLat)) * Math.sin(dLng / …
Run Code Online (Sandbox Code Playgroud)