我无法理解为什么这个非常简单的活动会产生内存泄漏.
它遵循此处给出的指导原则:https://developer.android.com/training/location/receive-location-updates.html以及此处给出的示例代码:https://github.com/googlesamples/android-play-location /树/主/ LocationUpdates
我还注意到,如果我不重写LocationCallback类的onLocationResult方法,则会解决内存泄漏问题.但就这种方式而言,LocationCallback完全没用.
谢谢您的帮助!
public class TestLeaks extends Activity {
private FusedLocationProviderClient mFusedLocationClient; // Access to Fused Loc. Provider API
private LocationRequest mLocationRequest; // Stores parameters for requests to F.L.P.Api
private LocationCallback mLocationCallback; // Callback for Location events
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_leaks);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
Location loc = locationResult.getLastLocation();
((TextView) findViewById(R.id.latitude)).setText(String.format(Locale.US, "%.6f", loc.getLatitude()));
((TextView) findViewById(R.id.longitude)).setText(String.format(Locale.US, …Run Code Online (Sandbox Code Playgroud) android memory-leaks google-api-client location-updates fusedlocationproviderclient