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

后端 未结 1 1503
天涯浪人
天涯浪人 2020-12-12 07:22

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

相关标签:
1条回答
  • 2020-12-12 08:10

    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.

    0 讨论(0)
提交回复
热议问题