LicenseChecker checkAccess leaks ServiceConnection

纵饮孤独 提交于 2019-12-03 23:25:14

问题


I am receiving this exception in LogCat every time I press the Back button in my app:

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

The code responsible for this leak in onCreate() is:

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

How do I get rid of this leak?

I tried not assigning MyLicenseCheckerCallback to a member, thinking perhaps when the activity goes onPause() the reference to the callback is responsible for the leak:

mChecker.checkAccess(new MyLicenseCheckerCallback());

But that didn't get rid of the leak.

Update: Thanks to @zapl's comment below, I looked at Google's 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;
    }
}

At first I thought that I may be neglecting to call it, but I double-checked and I am calling mChecker.onDestroy(); in my activity's onDestroy().

I also checked onDestroy() in LicenseChecker.java and it is calling 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();
}

So, what is really going on?

Is this a bug in LVL?


回答1:


I just got the same problem, and with your update and zapl's comment I figured up that the problem is the emulator you are using.

This Emulators don't have the Google Play APIs, and the LVL can't bind to the service, leaving a connection open, at the end LVL can't close it with the onDestroy call.

Just create a new AVD using Google APIs instead of Android x.x and try your code there, if you don´t find the Google APIs in the Target pulldown when creating the new AVD download it with the Android SDK Manager.




回答2:


I have also met the same problem later I got to know that i havn't added that android permission com.android.vending.CHECK_LICENSE . After correcting this my was problem is now solved. Try adding this line your android manifest

<uses-permission android:name="com.android.vending.CHECK_LICENSE" />



回答3:


Just put

mChecker.onDestroy();

on your onDestroymethod of the activity that declares and uses the mChecker.

While Google's code in LicenceChecker looks like this:

  public synchronized void onDestroy() {
        cleanupService();
        mHandler.getLooper().quit();
    }



回答4:


I don't know about google's LicenceChecker, but you should call StopService() before exit the Activity otherwise the service is still running and leaks memory.



来源:https://stackoverflow.com/questions/11992859/licensechecker-checkaccess-leaks-serviceconnection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!