chromecustomtab xamarin android MainActivity 泄露了最初绑定在这里的 ServiceConnection CustomTabsServiceConnectionImpl@43a61ad

chromecustomtab xamarin android MainActivity has leaked ServiceConnection CustomTabsServiceConnectionImpl@43a61ad that was originally bound here

我在我的 xamarin android 项目中使用了 chromecustomtab,我可以使用 CustomTabActivityManger class 绑定服务,但是在 class.[=12] 中没有取消绑定服务的选项=]

因为我没有解除绑定,它总是抛出内存泄漏错误。

我正在使用 Nuget - xamarin.Android.Support.CustomTabs 版本 26.1.0.1

代码如下

namespace Android.Support.CustomTabs
{
  public class CustomTabsActivityManager
  {
    public CustomTabsActivityManager(Activity parentActivity);

    public CustomTabsSession Session { get; }
    public Activity ParentActivity { get; }
    public CustomTabsClient Client { get; }

    public event CustomTabsServiceDisconnectedDelegate CustomTabsServiceDisconnected;
    public event CustomTabsServiceConnectedDelegate CustomTabsServiceConnected;
    public event ExtraCallbackDelegate ExtraCallback;
    public event NavigationEventDelegate NavigationEvent;

    public static CustomTabsActivityManager From(Activity parentActivity, string servicePackageName = null);
    public bool BindService(string servicePackageName = null);
    public void LaunchUrl(string url, CustomTabsIntent customTabsIntent = null);
    public bool MayLaunchUrl(string url, Bundle extras, List<string> otherLikelyUrls);
    public bool Warmup(long flags = 0);

    public class ExtraCallbackEventArgs
    {
        public ExtraCallbackEventArgs();

        public string CallbackName { get; set; }
        public Bundle Args { get; set; }
    }

    public delegate void NavigationEventDelegate(int navigationEvent, Bundle extras);
    public delegate void ExtraCallbackDelegate(object sender, ExtraCallbackEventArgs e);
    public delegate void CustomTabsServiceConnectedDelegate(ComponentName name, CustomTabsClient client);
    public delegate void CustomTabsServiceDisconnectedDelegate(ComponentName name);
}

I can bind service using CustomTabActivityManger class, but there is no option for unbind the service in that class

分析:

通常我们可以直接使用unbindService to unbind the Service. But in the CustomTabActivityManger源代码,但发现没有UnBindService()方法。而且您无法从外部获取 CustomTabsServiceConnection 实例,因此很难解除绑定 Activity:

中的服务
public class CustomTabsActivityManager
{
    ...
    CustomTabsServiceConnectionImpl connection;
    ...

    public bool BindService (string servicePackageName = null)
    {
        ...

        connection = new CustomTabsServiceConnectionImpl {
            CustomTabsServiceConnectedHandler = (name, client) => {
                Client = client;
                var evt = CustomTabsServiceConnected;
                if (evt != null)
                    evt (name, client);
            },
            OnServiceDisconnectedHandler = (name) => {
                var evt = CustomTabsServiceDisconnected;
                if (evt != null)
                    evt (name);
            }
        };

        return CustomTabsClient.BindCustomTabsService (ParentActivity, servicePackageName, connection);
    }
} 

class CustomTabsServiceConnectionImpl : CustomTabsServiceConnection
{
     ...
}

解法:

您可以创建自定义 CustomTabsActivityManager class 并添加 UnBindService() 方法:

public class MyCustomTabsActivityManager
{
     CustomTabsServiceConnectionImpl connection;

     public Activity ParentActivity { get; private set; }
     public CustomTabsClient Client { get; private set; }

     CustomTabsSession session = null;

     ...

     public void UnBindService()
     {
         if (connection != null)
         {
             ParentActivity.UnbindService(connection);
             Client = null;
             session = null;
          }
     }
}

然后,您可以在 Activity:

中使用此 UnBindService()
protected override void OnDestroy()
{
    myCustomTabsActivityManager.UnBindService();
    base.OnDestroy();
}