android - LicenseChecker checkAccess泄漏ServiceConnection

标签 android google-play android-lvl serviceconnection

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

Activity has leaked ServiceConnection com.android.vending.licensing.LicenseChecker@471cc039 that was originally bound here

onCreate() 中导致此泄漏的代码是:

mLicenseCheckerCallback = new MyLicenseCheckerCallback();
mChecker.checkAccess(mLicenseCheckerCallback);

如何消除这种泄漏?

我尝试不将 MyLicenseCheckerCallback 分配给成员,认为当 Activity 进行时 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;
    }
}

起初我以为我可能忽略了调用它,但我仔细检查了一下,我 am 在我的 Activity 的 中调用 mChecker.onDestroy(); >onDestroy().

我还在 LicenseChecker.java 中检查了 onDestroy(),它 正在 调用 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 中的错误吗?

最佳答案

我也遇到了同样的问题,根据您的更新和 zapl 的评论,我发现问题出在您使用的模拟器上。

此模拟器没有 Google Play API,LVL 无法绑定(bind)到服务,使连接保持打开状态,最后 LVL 无法通过 onDestroy 调用关闭它。

只需使用 Google APIs 而不是 Android x.x 创建一个新的 AVD 并在那里尝试您的代码,如果您在创建新的 AVD 时没有在 Target 下拉菜单中找到 Google APIs,请使用 Android SDK 管理器下载它。

关于android - LicenseChecker checkAccess泄漏ServiceConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11992859/

相关文章:

java - 图像加载器,无法连接到

android - 如何获取设备 token

java - 简单的 Android 许可

android - Wallpaper 中的许可验证库

android - LVL 为免费用户提供 LICENSED 响应

java - 如何知道 firebase 中的值是增加还是减少?

android - 为什么这个 WifiInfo 有时在 android 上为 null?

android - 安卓市场上的"Download declined"

android - 从我的 Android 应用程序链接到其他应用程序

android - 不同应用中的相同 SKU (Google Play)