Implementing an AccountManager in Xamarin.Android

1.1k views Asked by At

I have been following this guide in making an AccountManager for Android. But I am using Xamarin, and I have reached a point where the C# code is too different from the Java code. The account is on the phone, and I can add an account from the settings screen just fine. But when I try to access the AccountManager.AddAccount method externally, the arguments it takes are different and I do not think they are explained very well in the Xamarin documentation.

This page in the Xamarin documentation shows that the last two arguments in AddAccount are IAccountManagerCallback and Handler parameters. I am not sure how to go about implementing these in a way that I can pass them into AddAccount.

Just to be clear, what I want to do is call AddAccount (which is inside my custom AbstractAccountAuthenticator) from a different activity. When you click "Add Account" in the Android settings screen, it automatically calls the right function.

1

There are 1 answers

0
Stephen du Buis On

I see this was posted a while back but in case it is still of some help to someone this is how I implemented IAccountManagerCallback in Xamarin...

    internal static void AddAccount(Activity activity, string type) {
        AccountManager accountManager = AccountManager.Get(activity);
        IAccountManagerCallback callback = new AccountManagerCallback();
        accountManager.AddAccount(type, null, null, null, activity, callback, null);
    }
    private class AccountManagerCallback : Java.Lang.Object, IAccountManagerCallback {
        void IAccountManagerCallback.Run(IAccountManagerFuture future) {
            if (future.IsCancelled) {
                //task was cancelled code
            }
            else if (future.IsDone) {
                //task is completed
                Java.Lang.Object result = future.Result;
                //process result
            }
        }
    }