LicenseChecker checkAccess泄漏ServiceConnection

Bil*_*Ape 19 android serviceconnection android-lvl google-play

每次按下Back我的应用程序中的按钮,我都会在LogCat中收到此异常:

Activity已泄露最初绑定的ServiceConnection com.android.vending.licensing.LicenseChecker@471cc039

造成这种泄漏的代码onCreate()是:

mLicenseCheckerCallback = new MyLicenseCheckerCallback();
mChecker.checkAccess(mLicenseCheckerCallback);
Run Code Online (Sandbox Code Playgroud)

我如何摆脱这种泄漏?

我尝试不将MyLicenseCheckerCallback分配给成员,也许在活动开始时onPause()对回调的引用负责泄漏:

mChecker.checkAccess(new MyLicenseCheckerCallback());
Run Code Online (Sandbox Code Playgroud)

但这并没有摆脱泄漏.

更新:感谢@ 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

起初我以为我可能会忽略称它,但我仔细检查了,我正在调用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();
}
Run Code Online (Sandbox Code Playgroud)

那么,究竟发生了什么?

这是LVL中的一个错误吗?

pva*_*lle 5

我只是遇到了同样的问题,通过您的更新和zapl的评论,我发现问题出在您使用的仿真器上。

该仿真器没有Google Play API,LVL无法绑定到服务,从而保持连接打开,最后LVL无法通过onDestroy调用将其关闭。

只需使用Google API而非Android xx创建新的AVD,然后在此处尝试您的代码,如果在创建新的AVD时未在“目标”下拉列表中找到Google API,请通过Android SDK Manager下载。


use*_*184 0

就放

mChecker.onDestroy();
Run Code Online (Sandbox Code Playgroud)

onDestroy声明和使用 mChecker 的活动方法上。

虽然谷歌的代码LicenceChecker看起来像这样:

  public synchronized void onDestroy() {
        cleanupService();
        mHandler.getLooper().quit();
    }
Run Code Online (Sandbox Code Playgroud)