FusedLocationProviderClient是否泄漏内存?

时间:2018-07-06 06:41:21

标签: android memory-leaks location fusedlocationproviderclient

在我的一项活动中,我正在使用FusedLocationProviderClient获得持续的位置更新。我的代码基于以下方法:https://developer.android.com/training/location/receive-location-updates

在我的onCreate中,我设置了提供程序和回调

// setup fused location provider
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

    // build location request
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(30000);
    mLocationRequest.setFastestInterval(10000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setSmallestDisplacement(50);

    // Setup the callback function.
    mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                return;
            }
            for (Location location : locationResult.getLocations()) {
                // Update UI with location data
                // ...

                mCurrentLocation = location;
            }
        }
    };

在onResume中

mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest,
                                mLocationCallback,
                                Looper.myLooper());

处于暂停状态

mFusedLocationProviderClient.removeLocationUpdates(mLocationCallback);

但是由于某种原因,泄漏金丝雀仍指示存在内存泄漏。 泄漏的金丝雀日志如下所示 enter image description here

浏览堆栈溢出时,有一些帖子似乎表明泄漏是由于Google Play服务引起的。但是这些帖子都在谈论fusedLocationApi,而我正在使用熔融LocationProviderClient,因此我不确定它是否与我在这里使用的东西相同。有人可以帮我确认一下吗?谢谢!

2 个答案:

答案 0 :(得分:1)

我通过将LocationCallback的SoftReference传递给FusedLocationProvider修复了LeakCanary报告的泄漏。

public class LocationCallbackReference extends LocationCallback {

    private final SoftReference<LocationCallback> mLocationCallbackRef;

    public LocationCallbackReference(LocationCallback locationCallback) {
        mLocationCallbackRef = new SoftReference<>(locationCallback);
    }

    @Override
    public void onLocationResult(LocationResult locationResult) {
        super.onLocationResult(locationResult);
        if (mLocationCallbackRef.get() != null) {
            mLocationCallbackRef.get().onLocationResult(locationResult);
        }
    }

    @Override
    public void onLocationAvailability(LocationAvailability locationAvailability) {
        super.onLocationAvailability(locationAvailability);
        if (mLocationCallbackRef.get() != null) {
            mLocationCallbackRef.get().onLocationAvailability(locationAvailability);
        }
    }
}

我希望有帮助。

答案 1 :(得分:1)

对我来说,我使用LocationCallback作为内部匿名对象(因为我必须更新/访问包含的类数据),并且 FusedLocationProviderClient 导致内存泄漏。

然后,我将内部类重构为静态(非内部)类,并通过传递给静态LocationCallback对象的实时数据更新封闭类的数据。

 <label for="postDatatoDd">Post</label>
            <input type="text" id="postDatatoDd"
            ng-model="data.postDatatoDd"
                class="feed form-control" 
                placeholder="post your data here?" 
            />
            <button ng-click="postDatatoDd()" class="btn btn-primary">Post</button>

        </div>

}