在我的应用程序的一个片段中,我需要位置更新来确定用户何时靠近一个简短的位置列表,以便用户可以获得相关信息。
LocationServices.FusedLocationApi.requestLocationUpdates(client, LocationRequest.create(), this);
我还使用以下命令删除onPause()方法中的更新
LocationServices.FusedLocationApi.removeLocationUpdates(client, this);
然而,即使我调用该方法来删除位置更新,我还是会收到leak canary的警告,每当我导航离开该片段时(所有片段导航都是使用replace()事务完成的),我的片段正在泄漏。我在启动片段中的位置请求时是否做错了什么,或者我没有在onPause()中正确清理请求,对此的任何帮助都将非常感谢。
下面是我的fragment类的简化模型的代码
public class BlankFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks
, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private GoogleApiClient client;
private TextView textView;
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_blank, container, false);
textView = (TextView) root.findViewById(R.id.location);
client = new GoogleApiClient.Builder(getContext())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
return root;
}
@Override
public void onConnected(@Nullable Bundle bundle) {
startLocationUpdates();
}
private void startLocationUpdates() {
if (client.isConnected()) {
LocationServices.FusedLocationApi.requestLocationUpdates(client, LocationRequest.create(), this);
}
}
private void stopLocationUpdates(){
LocationServices.FusedLocationApi.removeLocationUpdates(client, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
textView.setText(location.toString());
}
@Override
public void onResume() {
startLocationUpdates();
super.onResume();
}
@Override
public void onPause() {
stopLocationUpdates();
super.onPause();
}
@Override
public void onStart() {
client.connect();
super.onStart();
}
@Override
public void onStop() {
if (client.isConnected()) {
client.disconnect();
}
super.onStop();
}}
这里还有生成的泄漏跟踪
In com.example.testlocation:1.0:1.
* com.example.testlocation.BlankFragment has leaked:
* GC ROOT com.google.android.gms.internal.zzary$zzb.zzaGN
* references com.google.android.gms.internal.zzary$9.zzbkw (anonymous subclass of com.google.android.gms.internal.zzary$zza)
* leaks com.example.testlocation.BlankFragment instance
* Retaining: 7.1 KB.
* Reference Key: 720085d5-af68-42f3-a291-ef959e4c8ffa
* Device: Huawei google Nexus 6P angler
* Android Version: 7.1.2 API: 25 LeakCanary: 1.5 00f37f5
* Durations: watch=5031ms, gc=167ms, heap dump=1418ms, analysis=137993ms
* Details:
* Instance of com.google.android.gms.internal.zzary$zzb
| static $classOverhead = byte[240]@316980881 (0x12e4be91)
| zzaGN = com.google.android.gms.internal.zzary$9@316955016 (0x12e45988)
| mDescriptor = java.lang.String@317419776 (0x12eb7100)
| mObject = 483616889632
| mOwner = com.google.android.gms.internal.zzary$zzb@317493408 (0x12ec90a0)
| shadow$_klass_ = com.google.android.gms.internal.zzary$zzb
| shadow$_monitor_ = 0
* Instance of com.google.android.gms.internal.zzary$9
| static $classOverhead = byte[312]@317158785 (0x12e77581)
| zzbkw = com.example.testlocation.BlankFragment@316908800 (0x12e3a500)
| zzaxf = com.google.android.gms.common.api.Api@317047488 (0x12e5c2c0)
| zzazY = com.google.android.gms.common.api.Api$zzf@315109896 (0x12c83208)
| zzK = true
| zzaAh = java.lang.Object@315110136 (0x12c832f8)
| zzaAi = com.google.android.gms.internal.zzaaf$zza@317493376 (0x12ec9080)
| zzaAj = java.lang.ref.WeakReference@317491784 (0x12ec8a48)
| zzaAk = java.util.ArrayList@317491760 (0x12ec8a30)
| zzaAl = null
| zzaAm = java.util.concurrent.atomic.AtomicReference@317340960 (0x12ea3d20)
| zzaAn = null
| zzaAo = false
| zzaAp = false
| zzaAq = null
| zzaAr = null
| zzaAs = false
| zzair = com.google.android.gms.common.api.Status@317061968 (0x12e5fb50)
| zzazt = com.google.android.gms.common.api.Status@317061968 (0x12e5fb50)
| zztj = java.util.concurrent.CountDownLatch@317340944 (0x12ea3d10)
| shadow$_klass_ = com.google.android.gms.internal.zzary$9
| shadow$_monitor_ = -1987877821
* Instance of com.example.testlocation.BlankFragment
| static serialVersionUID = 8326097604377836997
| static $change = null
| static $classOverhead = byte[1168]@315449345 (0x12cd6001)
| client = com.google.android.gms.internal.zzaat@317202944 (0x12e82200)
| textView = android.support.v7.widget.AppCompatTextView@317036544 (0x12e59800)
| mAdded = false
| mAllowEnterTransitionOverlap = null
| mAllowReturnTransitionOverlap = null
| mAnimatingAway = null
| mArguments = null
| mBackStackNesting = 0
| mCalled = true
| mCheckedForLoaderManager = false
| mChildFragmentManager = null
| mChildNonConfig = null
| mContainer = null
| mContainerId = 0
| mDeferStart = false
| mDetached = false
| mEnterTransition = null
| mEnterTransitionCallback = android.app.SharedElementCallback$1@1883261776 (0x70404b50)
| mExitTransition = null
| mExitTransitionCallback = android.app.SharedElementCallback$1@1883261776 (0x70404b50)
| mFragmentId = 0
| mFragmentManager = null
| mFromLayout = false
| mHasMenu = false
| mHidden = false
| mHost = null
| mInLayout = false
| mIndex = -1
| mLoaderManager = null
| mLoadersStarted = false
| mMenuVisible = true
| mNextAnim = 0
| mParentFragment = null
| mReenterTransition = android.transition.TransitionSet@1883202880 (0x703f6540)
| mRemoving = false
| mRestored = false
| mRetainInstance = false
| mRetaining = false
| mReturnTransition = android.transition.TransitionSet@1883202880 (0x703f6540)
| mSavedFragmentState = null
| mSavedViewState = android.util.SparseArray@317491880 (0x12ec8aa8)
| mSharedElementEnterTransition = null
| mSharedElementReturnTransition = android.transition.TransitionSet@1883202880 (0x703f6540)
| mState = 0
| mStateAfterAnimating = 0
| mTag = null
| mTarget = null
| mTargetIndex = -1
| mTargetRequestCode = 0
| mUserVisibleHint = true
| mView = null
| mWho = null
| shadow$_klass_ = com.example.testlocation.BlankFragment
| shadow$_monitor_ = -2047550015
* Excluded Refs:
| Field: android.view.Choreographer$FrameDisplayEventReceiver.mMessageQueue (always)
| Thread:FinalizerWatchdogDaemon (always)
| Thread:main (always)
| Thread:LeakCanary-Heap-Dump (always)
| Class:java.lang.ref.WeakReference (always)
| Class:java.lang.ref.SoftReference (always)
| Class:java.lang.ref.PhantomReference (always)
| Class:java.lang.ref.Finalizer (always)
| Class:java.lang.ref.FinalizerReference (always)
2条答案
按热度按时间f1tvaqid1#
这是Google Maps SDK内存泄漏的一个长期问题,即使在调用removeLocationUpdates后也会发生。您可以在here上了解更多信息。
以下解决方案为我删除了泄漏金丝雀通知。
1)创建一个WeakLocationListener类,将位置侦听器 Package 在弱引用中,然后使用该类处理onLocationChanged回调。
2)创建一个WeakLocationListener的示例(使用由您的片段实现的LocationListener),并在您请求LocationUpdates和removeLocationUpdates时使用它--位置将返回给您的Activity/片段(或任何实现LocationListener的类),并且泄漏金丝雀通知应该消失。
希望这对你有帮助。
jmp7cifd2#
我通过使用LocationCallback的弱引用修复了LeakCanary报告的漏洞。这是Prezbo先生为我提供的真正有效的解决方案。但我在初始化弱引用时遇到了问题。我必须将其放入onResume方法中,之后它就像魅力一样工作。在任何需要的地方,我都通过
locationCallback.get()
来放置引用