I guess some of the code may help (I've removed what didn't seem relevant):

Service, Service Connection and Binder:

namespace Services
{
    [Service]
    public class LoginService: IntentService
    {
        public event Action LoggedIn;
        public event Action<String> LogInFailed;
        public event Action<String> LogInCrashed; 

        LoginServiceBinder Binder { get; set; }

        public LoginService()
            : base()
        {
            Binder = new LoginServiceBinder(this); 
        }

        public override IBinder OnBind(Intent intent)
        {
            base.OnBind(intent);
            return Binder;
        }

        protected override void OnHandleIntent(Intent intent)
        {
            try
            {
                ....Does some work here to log the user in...
                
            }
            catch (Exception ex)
            {
                if (LogInCrashed != null) LogInCrashed(ex.Message);
            }
            finally
            {
                StopSelf();
            }
        }

     
    }

    public class LoginServiceBinder: Binder
    {
        public LoginService Service { get; private set; }

        public LoginServiceBinder(LoginService service)
        {
            Service = service; 
        }
    }

    public class LoginServiceConnection : Java.Lang.Object,
IServiceConnection
    {
        public event Action<LoginService> Connected;
        public event Action Disconnected;

        public void OnServiceConnected(ComponentName className, IBinder
binder)
        {
            if (Connected != null) Connected((binder as
LoginServiceBinder).Service); 
        }

        public void OnServiceDisconnected(ComponentName className)
        {
            if (Disconnected != null) Disconnected(); 
        }
    }
}

Activity: 


    public class LoginActivity : Activity
    {
        private ProgressDialog ProgressDialog { get; set; }

        private Services.LoginServiceConnection LoginServiceConnection;
        private Boolean LoginServiceBound;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            //Use UI created in Main.axml
            SetContentView(Resource.Layout.Login);
            
            SetUpLoginService();
            
            //Set up login button
            Button login = FindViewById<Button>(Resource.Id.Login);
            login.Click += Login;
        }

        protected override void OnPause()
        {
            if (LoginServiceBound)
            {
                UnbindService(LoginServiceConnection);
            }
            base.OnPause();
        }

        protected override void OnResume()
        {
            base.OnResume();
        }

        protected override void OnDestroy()
        {
            if (LoginServiceBound)
            {
                UnbindService(LoginServiceConnection);
            }
            base.OnDestroy();
        }

      
        private void SetUpLoginService()
        {
            LoginServiceConnection = new Services.LoginServiceConnection();
            LoginServiceConnection.Connected += (Services.LoginService
service) =>
            {
                Android.Util.Log.Info("App", "Bound to checkin service");

                service.LoggedIn += () =>
                {
                    RunOnUiThread(() =>
                        {
                            ProgressDialogDismiss();
                            //Updated user works so carry on to the
homepage.
                            GoHome();
                        
                        });
                };

                service.LogInFailed += (message) =>
                {
                    RunOnUiThread(() =>
                    {
                        ProgressDialogDismiss();
                       
FindViewById<TextView>(Resource.Id.LoginWarning).Text = message;
                        Toast.MakeText(this, message,
ToastLength.Long).Show();
                    });
                };

                service.LogInCrashed += (message) =>
                {
                    RunOnUiThread(() =>
                    {
                        ProgressDialogDismiss();
                        Toast.MakeText(this, message,
ToastLength.Long).Show(); 
                    });
                };
            };

            LoginServiceConnection.Disconnected += () =>
            {
                RunOnUiThread(() =>
                {
                    LoginServiceBound = false;
                });
            };
        }



        protected void Login(Object sender, EventArgs e)
        {
            ProgressDialog = Android.App.ProgressDialog.Show(this,
GetString(Resource.String.please_wait),
GetString(Resource.String.logging_in), true);

            AutoCompleteTextView txtUsername =
FindViewById<AutoCompleteTextView>(Resource.Id.username);
            EditText txtPassword =
FindViewById<EditText>(Resource.Id.password);

            var login = new Intent(this, typeof(Services.LoginService))
                .PutExtra("Username", txtUsername.Text)
                .PutExtra("Password", txtPassword.Text);

            ProgressDialog.Show();

            StartService(login);
            BindService(new Intent(this, typeof(Services.LoginService)),
LoginServiceConnection, Bind.AdjustWithActivity);  
        }

        private void ProgressDialogDismiss()
        {
            if (ProgressDialog != null)
            {
                try
                {
                    ProgressDialog.Dismiss();
                }
                catch (Exception ex)
                {
                    Android.Util.Log.Error("App", ex.Message);
                    Toast.MakeText(this, ex.Message,
ToastLength.Long).Show();
                }
            }
        }
    }



--
View this message in context: 
http://mono-for-android.1047100.n5.nabble.com/Activity-leaking-Service-Connection-tp5711968p5711969.html
Sent from the Mono for Android mailing list archive at Nabble.com.
_______________________________________________
Monodroid mailing list
[email protected]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to