0
Hey, vor kurzem habe ich Leak-Canary zu meiner App hinzugefügt und ich bekomme dieses seltsame Leck auf meiner GPSTracker
Klasse.Lecks com.myapp.MainActivity Instanz 6.9mb
Leak Bericht sagt:
02-07 17:42:35.524 25788-26863/com.myapp D/LeakCanary: * com.myapp.MainActivity has leaked:
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * GC ROOT android.location.LocationManager$ListenerTransport.this$0
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * references android.location.LocationManager.mContext
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * references android.app.ContextImpl.mOuterContext
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * leaks com.myapp .MainActivity instance
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Retaining: 6.9 MB.
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Reference Key: d4781b46-49a2-426c-b2ba-15b9a1207dd6
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Device: LGE google Nexus 5 hammerhead
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Android Version: 6.0.1 API: 23 LeakCanary: 1.5 00f37f5
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Durations: watch=5106ms, gc=156ms, heap dump=5555ms, analysis=32364ms
02-07 17:42:35.525 25788-26863/com.myapp D/LeakCanary: * Instance of android.location.LocationManager$ListenerTransport
Und mein GPSTracker Klasse ist
public class GPSTracker implements LocationListener {
private Context context;
private boolean isGPSEnabled = false;
private boolean canGetLocation = false;
private Location location; // location
private double latitude; // latitude
private double longitude; // longitude
private final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 5; // 5 meters
private final long MIN_TIME_BW_UPDATES = 1500; // 15 sec
private LocationManager locationManager;
public GPSTracker(Context context) {
this.context = context;
getLocation();
}
public boolean getGpsStatus() {
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
return isGPSEnabled;
}
public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
Log.i("No Network Provider", "No Network Provider Available");
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, GPSTracker.this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.i("gps", "Location got from NETWORK");
Log.e("Current Location", "Lat : " + latitude + " Long : " + longitude);
}
}
} else {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, GPSTracker.this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.i("gps", "Location got from GPS");
Log.e("Current Location", "Lat : " + latitude + " Long : " + longitude);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
@Override
public void onLocationChanged(Location location) {
this.location = location;
try {
Log.e("GPS Status", "Lat : " + location.getLatitude() + " Long : " + location.getLatitude());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
onLocationChanged(locationManager.getLastKnownLocation(provider));
locationManager.requestLocationUpdates(provider, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
protected void finalize() throws Throwable {
removeLocationListener();
super.finalize();
}
public void removeLocationListener() {
context=null;
locationManager.removeUpdates(GPSTracker.this);
locationManager=null;
location=null;
}
}
Und Sie können feststellen, dass ich zu removeUpdates versucht haben, wenn die MainActivity
zerstört. Aber immer noch hat sich nichts geändert, das gleiche Leck immer wieder zu haben.
Verwirrt!
Tadda! Das funktioniert für mich! –