Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

android - Why is my AccountAuthenticatorActivity not launching when triggered by another app?

I have a suite of apps and my AccountManager files live in one central app. I can use AccountManager.AddAccount() in that central app, but when I try to use that method from the other apps, the AuthenticatorActivity is not started. I can debug and see that all of the code from AddAccount is being executed, but the activity is never launched.

Here is my AddAccount method:

public override Bundle AddAccount(AccountAuthenticatorResponse response, string accountType, string authTokenType, string[] requiredFeatures, Bundle options)
{
    var intent = new Intent(_context, typeof(MyAccountAuthenticatorActivity));
    intent.PutExtra(MyAccountAuthenticatorActivity.ARG_ACCOUNT_TYPE, accountType);
    intent.PutExtra(MyAccountAuthenticatorActivity.ARG_AUTH_TYPE, authTokenType);
    intent.PutExtra(MyAccountAuthenticatorActivity.ARG_IS_ADDING_NEW_ACCOUNT, true);
    intent.PutExtra(AccountManager.KeyAccountAuthenticatorResponse, response);

    var bundle = new Bundle();
    bundle.PutParcelable(AccountManager.KeyIntent, intent);
    return bundle;
}

I use the same splash screen in all of my apps, so the code that calls AddAccount is the same.

        _accountManager = AccountManager.Get(this);
        var accounts = _accountManager.GetAccountsByType(AccountKeys.ACCOUNT_TYPE);

        //automatically add new account if no users on device yet
        if (accounts.Length == 0)
        {
            _accountManager.AddAccount(AccountKeys.ACCOUNT_TYPE, AccountKeys.AUTH_TYPE, null, null, this, null, null);
            CheckIfFirstRun();
            Finish();
        }

MyAccountAuthenticatorActivity is located in one app. As you can see, I am sending in the correct activity context to AddAccount but StartActivity() is only called when those code is executed in the app that owns the authenticator files.

What else am I missing to allow my other apps to open up MyAccountAuthenticatorActivity? Could it be possible that it has to do with setting the callback to null when I call AddAccount? I cannot figure out how to do this another way as I do not fully understand how to use the callback because none of the java examples have this.

I have also tried adding the MyAccountAuthenticatorActivity to the manifest of my other app like so:

<activity android:name="com.redacted.authenticator.MyAccountAuthenticatorActivity" />

But this doesn't change anything. I know the other apps are seeing the AuthenticatorService, they just won't launch the activity.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So I could not figure out why AddAccount() would not launch the activity itself, but I was able to find a workaround. I was able to just handle the intent myself.

This is my code snippet where I begin adding a new account (from any app):

            var adapter = new AccountPickerArrayAdapter(this, accounts);
            var builder = new AlertDialog.Builder(new ContextThemeWrapper(this, Resource.Style.AppTheme));
            builder.SetTitle(Resource.String.choose_account);
            builder.SetAdapter(adapter,
                (s, a) =>
                {
                    var dialog = (AlertDialog)s;
                    dialog.Dismiss();
                    GetExistingAuthToken(accounts[a.Which]);
                    FinishLogin(accounts[a.Which]);
                });
            builder.SetNeutralButton(Resource.String.add_new_account,
                (s, e) =>
                {
                    var dialog = (AlertDialog)s;
                    dialog.Dismiss();
                    var thread = new Thread(AddNewAccount);
                    thread.Start();
                    CheckIfFirstRun();
                    Finish();
                });
            builder.Create().Show();
        }

        void AddNewAccount()
        {
            var future = _accountManager.AddAccount(AccountKeys.ACCOUNT_TYPE, AccountKeys.AUTH_TYPE, null, null, null, null, null);
            var bundle = future.Result as Bundle;
            if (bundle != null)
            {
                var intent = bundle.GetParcelable(AccountManager.KeyIntent) as Intent;
                StartActivity(intent);
            }
        }

By sending nullas the Activityin AddAccount(), the desired intent is returned in a bundle. I can then just launch that intent directly.

I also needed to add (Exported = true) to the manifest entry for my AccountAuthenticatorActivity. This lets other apps launch that activity.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...