本文介绍了为什么我的AccountAuthenticatorActivity在被另一个应用触发后无法启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一套应用程序,我的AccountManager文件位于一个中央应用程序中.我可以在该中央应用程序中使用AccountManager.AddAccount(),但是当我尝试从其他应用程序中使用该方法时,AuthenticatorActivity无法启动.我可以调试并看到AddAccount中的所有代码都在执行,但是从未启动该活动.

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.

这是我的AddAccount方法:

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;
}

我在所有应用程序中都使用了相同的启动屏幕,因此调用AddAccount的代码是相同的.

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位于一个应用程序中.如您所见,我将正确的活动上下文发送到AddAccount,但是只有在拥有身份验证文件的应用中执行这些代码时,才会调用StartActivity().

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.

我还缺少什么允许我的其他应用打开MyAccountAuthenticatorActivity?调用AddAccount时,可能与将回调设置为null有关吗?我无法弄清楚如何以其他方式执行此操作,因为我不完全了解如何使用回调,因为没有Java示例具有此功能.

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.

我还尝试过将MyAccountAuthenticatorActivity添加到其他应用程序的清单中,如下所示:

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

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

但这不会改变任何东西.我知道其他应用程序都在使用AuthenticatorService,但它们不会启动该活动.

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

推荐答案

所以我不知道为什么AddAccount()不能启动活动本身,但是我找到了解决方法.我能够自己解决意图.

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);
            }
        }

通过发送null作为AddAccount()中的Activity,所需的意图以捆绑形式返回.然后,我可以直接启动该意图.

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

我还需要将(Exported = true)添加到AccountAuthenticatorActivity的清单条目中.这使其他应用程序可以启动该活动.

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

这篇关于为什么我的AccountAuthenticatorActivity在被另一个应用触发后无法启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 08:40