Shmukko
Shmukko

Reputation: 592

Error android.app.ServiceConnectionLeaked when InApp purchase

I am developing an Android App with Xamarin Android. I'm using the InAppBilling Plugin from James Montemagno.

When I call PurchaseAsync Method the PlayStore dialog opens. But in the background my app freezes and I dont get any result.

var purchase = await CrossInAppBilling.Current.PurchaseAsync(productId, ItemType.Subscription, "apppayload");

In the Sys Log theres a android.app.ServiceConnectionLeaked error:

02-17 22:13:05.434  LENOVO YT3-X50L Error   8031    ActivityThread  android.app.ServiceConnectionLeaked: Activity md5742c3bd4cdfedb6330d25c53207d662c.ShopActivity has leaked ServiceConnection md57a6f08dbc6561d468b2675b2ac9edab2.InAppBillingImplementation_InAppBillingServiceConnection@2277a40 that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1092)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:986)
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1303)
at android.app.ContextImpl.bindService(ContextImpl.java:1286)
at android.content.ContextWrapper.bindService(ContextWrapper.java:604)
at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:30)
at android.view.View.performClick(View.java:5205)
at android.view.View$PerformClick.run(View.java:21164)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I only get this error on LENOVO YT3-X50L. On Samsung phones it works fine...

Upvotes: 2

Views: 458

Answers (1)

SushiHangover
SushiHangover

Reputation: 74144

ServiceConnectionLeaked

The CrossInAppBilling code does not take into consideration that your Activity can be killed/flushed from memory due to memory pressure/requirements and does not directly do anything to try to prevent it, it is up the app developer to determine when they need to take special action.

This is something I see a lot of in lower-end Android devices and the the new Android Oreo Go (<=1MB) test devices, but can happen on any device, but mostly noticeable on 2GB and lower devices.

  • Before calling any external code flush/release as much memory as possible

    • Focusing on releasing images is usually the largest payoff for memory reduction (restore them after the purchase is completed)

    • In cases of the Oreo Go 512MB devices I have had to go the extra mile and finish the current Activity, create a new blank/empty transient Activity and then call the external code (app billing, camera, etc) and upon completion, restore the original activity and bring it up to date with the new information externally obtained.

Note: profile your app and the activity first in order to focus your time.

  • Use your own Keep-Alive Service

    • Using Start/StopService and not Bind/UnBindService
    • Make it a Foreground Service

Note: This does not prevent the OS from killing/flushing your Activities/Services, it just provides a "hint" that it should not...

Note: Monitor the Importance state within the RunningAppProcessInfo to determine if your app is entering ReasonServiceInUse before you execute the external code.

Upvotes: 2

Related Questions