每次按下Back
应用程序中的按钮时,我都会在 LogCat 中收到此异常:
Activity 泄露了原来绑定在这里的ServiceConnection com.android.vending.licensing.LicenseChecker@471cc039
负责此泄漏的代码onCreate()
是:
mLicenseCheckerCallback = new MyLicenseCheckerCallback();
mChecker.checkAccess(mLicenseCheckerCallback);
我该如何摆脱这种泄漏?
我尝试不将 MyLicenseCheckerCallback 分配给成员,认为当活动进行时onPause()
,对回调的引用可能会导致泄漏:
mChecker.checkAccess(new MyLicenseCheckerCallback());
但这并没有消除泄漏。
更新:感谢@zapl 在下面的评论,我查看了 Google 的LicenseChecker.java
:
/** Unbinds service if necessary and removes reference to it. */
private void cleanupService() {
if (mService != null) {
try {
mContext.unbindService(this);
} catch (IllegalArgumentException e) {
// Somehow we've already been unbound. This is a non-fatal error.
Log.e(TAG, "Unable to unbind from licensing service (already unbound)");
}
mService = null;
}
}
起初我以为我可能忽略了调用它,但我仔细检查并调用了我mChecker.onDestroy();
的活动onDestroy()
。
我也签到onDestroy()
了LicenseChecker.java
,它正在调用unbindService
:
/**
* Inform the library that the context is about to be destroyed, so that
* any open connections can be cleaned up.
* <p>
* Failure to call this method can result in a crash under certain
* circumstances, such as during screen rotation if an Activity requests
* the license check or when the user exits the application.
*/
public synchronized void onDestroy() {
cleanupService();
mHandler.getLooper().quit();
}
那么,到底发生了什么?
这是 LVL 中的错误吗?