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

☆樱花仙子☆ 提交于 2019-11-28 14:38:23

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!