为什么我的 AccountAuthenticatorActivity 在被另一个应用程序触发时没有启动? [英] Why is my AccountAuthenticatorActivity not launching when triggered by another app?

查看:33
本文介绍了为什么我的 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);
            }
        }

通过在 AddAccount() 中将 null 作为 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 在被另一个应用程序触发时没有启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆