Activity <Name> has leaked ServiceConnection com.google.android.vending.licensing.LicenseChecker that was originally bound here

StackOverflow https://stackoverflow.com/questions/13443261

  •  30-11-2021
  •  | 
  •  

Question

I'm trying to apply the ServerManagedPolicy to my application before publishing it. I've defined a byte[] array for the Salt, got my public key, got the getPackageName() and the device id from Settings.Secure.ANDROID_ID. I've also defined a private class within my main Activity holding a LicenseCheckerCallback implementation. Let's talk about code:

Within the onCreate():

public void onCreate(Bundle savedInstanceState) { // line 47
    String deviceId = Settings.Secure.getString(getContentResolver(),Settings.Secure.ANDROID_ID);

    checker = new LicenseChecker(this, new ServerManagedPolicy(this,
        new AESObfuscator(SALT, getPackageName(), deviceId)),
        BASE64_PUBLIC_KEY
    );
    checkerCallback = new MyCheckerCallback();

    checker.checkAccess(checkerCallback); // line 56

Inside the onDestroy():

@Override
protected void onDestroy() { // line 125
    super.onDestroy();
    checker.onDestroy();
}

MyCheckerCallback:

private class MyCheckerCallback implements LicenseCheckerCallback {

    @Override
    public void allow(int reason) {
        if (isFinishing()) {
            return;
        }
        displayResult("Allow");
    }

    @Override
    public void dontAllow(int reason) {
        if (isFinishing()) {
            return;
        }
        displayResult("Don't allow");

        if (reason == ServerManagedPolicy.RETRY) {
            displayResult("Retry");
        } else {
            displayResult("Go to market");
        }
    }

    @Override
    public void applicationError(int errorCode) {

        displayResult((errorCode==LicenseCheckerCallback.ERROR_CHECK_IN_PROGRESS)+" progress");
        displayResult((errorCode==LicenseCheckerCallback.ERROR_INVALID_PACKAGE_NAME)+" package");
        displayResult((errorCode==LicenseCheckerCallback.ERROR_INVALID_PUBLIC_KEY)+" public");
        displayResult((errorCode==LicenseCheckerCallback.ERROR_MISSING_PERMISSION)+" permission");
        displayResult((errorCode==LicenseCheckerCallback.ERROR_NON_MATCHING_UID)+" uid");
        displayResult((errorCode==LicenseCheckerCallback.ERROR_NOT_MARKET_MANAGED)+" market");
    }
}

It is based entirely on this page from Developer tutorials. displayResult() shows an alert with a message:

private void displayResult(String message) {
    AlertDialog dialog = new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.ic_dialog_alert).setTitle("Status")
            .setMessage(message)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).create();
    dialog.show();
}

After setting up all this code, it returns a couple of dialogs: Don't allow, and Retry, and in some cases, after stressing a little the app, it shows up this error in the Log.

11-18 18:50:17.566: E/ActivityThread(23675): Activity com.package.test.TestActivity has leaked ServiceConnection com.google.android.vending.licensing.LicenseChecker@41a74ad0 that was originally bound here
11-18 18:50:17.566: E/ActivityThread(23675): android.app.ServiceConnectionLeaked: Activity com.package.test.TestActivity has leaked ServiceConnection com.google.android.vending.licensing.LicenseChecker@41a74ad0 that was originally bound here
11-18 18:50:17.566: E/ActivityThread(23675):    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:965)
11-18 18:50:17.566: E/ActivityThread(23675):    at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:859)
11-18 18:50:17.566: E/ActivityThread(23675):    at android.app.ContextImpl.bindService(ContextImpl.java:1191)
11-18 18:50:17.566: E/ActivityThread(23675):    at android.app.ContextImpl.bindService(ContextImpl.java:1183)
11-18 18:50:17.566: E/ActivityThread(23675):    at android.content.ContextWrapper.bindService(ContextWrapper.java:394)
11-18 18:50:17.566: E/ActivityThread(23675):    at com.google.android.vending.licensing.LicenseChecker.checkAccess(LicenseChecker.java:150)
11-18 18:50:17.566: E/ActivityThread(23675):    at com.package.test.TestActivity.onCreate(TestActivity.java:56)
11-18 18:50:17.566: E/ActivityThread(23675):    at android.app.Activity.performCreate(Activity.java:5008)
11-18 18:50:17.566: E/ActivityThread(23675):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-18 18:50:17.566: E/ActivityThread(23675):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-18 18:50:17.566: E/ActivityThread(23675):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-18 18:50:17.566: E/ActivityThread(23675):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3512)
11-18 18:50:17.566: E/ActivityThread(23675):    at android.app.ActivityThread.access$700(ActivityThread.java:130)
11-18 18:50:17.566: E/ActivityThread(23675):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
11-18 18:50:17.566: E/ActivityThread(23675):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 18:50:17.566: E/ActivityThread(23675):    at android.os.Looper.loop(Looper.java:137)
11-18 18:50:17.566: E/ActivityThread(23675):    at android.app.ActivityThread.main(ActivityThread.java:4745)
11-18 18:50:17.566: E/ActivityThread(23675):    at java.lang.reflect.Method.invokeNative(Native Method)
11-18 18:50:17.566: E/ActivityThread(23675):    at java.lang.reflect.Method.invoke(Method.java:511)
11-18 18:50:17.566: E/ActivityThread(23675):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-18 18:50:17.566: E/ActivityThread(23675):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-18 18:50:17.566: E/ActivityThread(23675):    at dalvik.system.NativeStart.main(Native Method)

Have you ever faced this error before?

Was it helpful?

Solution

Ahhh, just my luck. Finally, I got it working. The problem with the Retry was that the Developer console was set to return the regular answer a while ago (like an hour or so), and the device was only connected to 3G. It didn't properly connect to the server. Once I moved to Wi-Fi, the responses started to be more useful (NOT_MARKET_MANAGED, in my case, as the app was not uploaded yet).

The second problem, the logged error one, appeared just when turning the device with a Dialog open.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top