chromecustomtab xamarin android MainActivity已泄漏了最初绑定在此处的ServiceConnection CustomTabsServiceConnectionImpl @ 43a61ad

时间:2018-11-22 17:15:22

标签: xamarin.android chrome-custom-tabs

我在xamarin android项目中使用了chromecustomtab,我可以使用CustomTabActivityManger类来绑定服务,但是在该类中没有取消绑定服务的选项。

由于我没有解除绑定的约束,所以总是会向我抛出内存泄漏错误。

我正在使用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);
}

1 个答案:

答案 0 :(得分:1)

  

我可以使用CustomTabActivityManger类来绑定服务,但是在该类中没有取消绑定服务的选项

分析:

通常,我们可以直接使用unbindService来解除Service的绑定。但是在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类并添加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;
          }
     }
}

然后,您可以在UnBindService()中使用此Activity

protected override void OnDestroy()
{
    myCustomTabsActivityManager.UnBindService();
    base.OnDestroy();
}