简体   繁体   English

如何使AccountManager每个用户名处理多个帐户?

[英]How can I make AccountManager handle multiple accounts per username?

I would like to use Android's built-in AccountManager to handle accounts for our native Android app. 我想使用Android内置的AccountManager处理我们的本机Android应用程序的帐户。 However, we have a peculiar need. 但是,我们有一个特殊的需求。

It appears that Android's concept of an Account is a combination of name (ie MazerRackham@example.com) and type (ie com.example). Android的“帐户”概念似乎是名称(即MazerRackham@example.com)和类型(即com.example)的组合。 Name is the username you login with. 名称是您用来登录的用户名。 Type is associated with your application or company. 类型与您的应用程序或公司相关联。

However, our REST backend allows a single user to have multiple accounts and each account must be accessed by its own unique hash tied to the combination of one username and one account number (in addition to type). 但是,我们的REST后端允许单个用户拥有多个帐户,并且每个帐户都必须通过其自己的唯一哈希访问,该哈希与一个用户名和一个帐号(除了类型)组合在一起。

I already have the AccountAuthenticator, AuthenticationService, and AccountAuthenticatorActivity working for a single user with a single account number and single stored hash. 我已经有AccountAuthenticator,AuthenticationService和AccountAuthenticatorActivity为具有单个帐号和单个存储的哈希的单个用户工作。 Is there any way in which I could implement my AccountAuthenticator to handle users with multiple accounts under the same username, each requiring a hash? 有什么方法可以实现AccountAuthenticator来处理使用相同用户名使用多个帐户的用户,每个帐户都需要一个哈希? Would it be possible to attach a delimiter to the username and split it into username and account number every time I use it? 是否可以在用户名上附加定界符,并在每次使用时将其分为用户名和帐号?

If I cannot figure out a way to handle this, how should I gracefully fall back during AbstractAccountAuthenticator.getAuthToken? 如果我找不到解决该问题的方法,应该如何在AbstractAccountAuthenticator.getAuthToken期间优雅地回退? Would it make sense to set the hash to a special flag value and use legacy login methods for that user? 将哈希设置为特殊标志值并为该用户使用旧版登录方法是否有意义? Or is that too much of a hack? 还是太多了?

Thanks for your help! 谢谢你的帮助!

如果您不介意哈希是公共的,则可以肯定地将帐户名设置为username|hash (或所需的任何分隔符)-系统不关心您用于帐户名的内容,只是它唯一地定义了一个用户。

I ended up serializing the data into the username using an at sign (@) as a delimiter. 我最终使用at符号(@)作为分隔符将数据序列化为用户名。 I chose the at sign because it's the only restricted character in an e-mail address that should only be used exactly one time. 我选择了at符号,因为它是电子邮件地址中唯一只能使用一次的受限字符。

Here is the code from getAuthToken in my AccountAuthenticator which gets called only if I need to obtain a fresh token for that user and account id: 这是我的AccountAuthenticator中的getAuthToken中的代码,仅当我需要获取该用户和帐户ID的新令牌时才调用该代码:

    /*
    *
    * The login params need to handle users with multiple accounts under the same username.
    *
    * Since Android's AccountManager does not allow multiple accounts per username, I had
    * to create a hack which joins and splits the username on a delimiter to serialize the
    * data and retrieve the account number for users with multiple accounts. I chose the @
    * sign as a delimiter because e-mail addresses have VERY few invalid characters in
    * the account name part of the address.
    *
    * If the user has multiple accounts, we need to create each one in AccountManager.
    *
    * */

    String[] accountParts = account.name.split("@");
    numParts = accountParts.length;
    if (numParts<2) {
        Log.wtf(Config.TAG, "Username split produced too few parts. WTF.");
        return null;
    }
    String email = accountParts[0] + "@" + accountParts[1];

    if (numParts==3) {
        String account_id = accountParts[2];
    } else if (numParts>3) {
        Log.wtf(Config.TAG, "Username split produced too many parts. WTF.");
        return null;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM