【问题标题】:Add a google account by code通过代码添加谷歌帐户
【发布时间】:2012-07-13 07:56:18
【问题描述】:

如何通过代码添加谷歌账号?

我只需要添加一个现有帐户,而不是创建一个新帐户。交互过程类似于设置> 帐户> 添加帐户

谢谢!

【问题讨论】:

    标签: android add account


    【解决方案1】:

    您必须设计您的用户界面,但添加现有帐户的最简单代码是这样的:

    AccountMager mgr = (AccountManager)getSystemService(ACCOUNT_SERVICE);
    Account acc = new Account("user@domain.com", "com.google"));
    if(mgr.addAccountExplicitly(acc, "password", new Bundle())) {
        //account added successfully
        //do whatever is needed;
    }
    else {
        //something did not work
    }
    

    您需要AUTHENTICATE_ACCOUNTS 权限。如果您通过null 代替密码,则将添加没有密码的帐户,并在下次重新同步时提示用户输入密码。

    如果您需要对流程进行更多控制,则可以使用方法

    public AccountManagerFuture<Bundle> addAccount (String accountType,
                                                    String authTokenType,
                                                    String[] requiredFeatures,
                                                    Bundle addAccountOptions,
                                                    Activity activity,
                                                    AccountManagerCallback<Bundle> callback,
                                                    Handler handler)
    

    在课堂上AccountManager。查看AccountManager class documentation了解更多详情。

    【讨论】:

    • 感谢您的回答。我收到以下异常:E/AndroidRuntime(1583): java.lang.SecurityException: caller uid 10009 is different than the authenticationator's uid
    • 看看this question - 它可能与“com.google”字符串有关。最好的办法是使用其中一个 android 类中的常量,但我不记得它在哪里。
    • 我认为“不同于身份验证器的 uid”例外是说您不能创建“com.google”帐户,因为您不是 Google。换句话说,原来的问题是不可能的。如果我错了,请纠正我!
    猜你喜欢
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多