首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓AccountManager addAccount服务

安卓AccountManager addAccount服务
EN

Stack Overflow用户
提问于 2016-12-24 12:42:42
回答 1查看 1.7K关注 0票数 0

我在服务中使用一个帐户管理器帐户(它扩展了FirebaseInstanceIdService)。如果没有有效帐户,则使用accountManager.addAccount添加帐户。

这将作为参数活动(用于启动帐户登录活动)。但是,当我从一个服务调用addAccount时,我没有当前的活动要放在那里。如何从服务中调用addAccount并让它在需要的地方显示帐户登录?

代码语言:javascript
复制
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    @Override
    public void onTokenRefresh() {
        AccountManager accountManager = (AccountManager) getApplicationContext().getSystemService(ACCOUNT_SERVICE);
        Account account[] = accountManager.getAccountsByType(ACCOUNT_TYPE);
        if(account.length==0) {
            Activity activity=???????//What can I set here
            accountManager.addAccount(ACCOUNT_TYPE, AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS, null,
            null, activity, new AccountManagerCallback<Bundle>() {
                        public void run(AccountManagerFuture<Bundle> arg0) {
                        }
                    }, null);
            return null;
        }
        //do stuff with ContentResolver using account
    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-12-24 19:54:36

addAccount()方法需要活动实例来调用默认身份验证活动。

它们是addAccountExplicitly()类中的另一个方法,称为AccountManager类。以下是文件:

代码语言:javascript
复制
/**
     * Adds an account directly to the AccountManager. Normally used by sign-up
     * wizards associated with authenticators, not directly by applications.
     * <p>Calling this method does not update the last authenticated timestamp,
     * referred by {@link #KEY_LAST_AUTHENTICATED_TIME}. To update it, call
     * {@link #notifyAccountAuthenticated(Account)} after getting success.
     * However, if this method is called when it is triggered by addAccount() or
     * addAccountAsUser() or similar functions, then there is no need to update
     * timestamp manually as it is updated automatically by framework on
     * successful completion of the mentioned functions.
     * <p>It is safe to call this method from the main thread.
     * <p>This method requires the caller to have a signature match with the
     * authenticator that owns the specified account.
     *
     * <p><b>NOTE:</b> If targeting your app to work on API level 22 and before,
     * AUTHENTICATE_ACCOUNTS permission is needed for those platforms. See docs
     * for this function in API level 22.
     *
     * @param account The {@link Account} to add
     * @param password The password to associate with the account, null for none
     * @param userdata String values to use for the account's userdata, null for
     *            none
     * @return True if the account was successfully added, false if the account
     *         already exists, the account is null, or another error occurs.
     */
    public boolean addAccountExplicitly(Account account, String password, Bundle userdata); 

使用:

创建帐户实例:

代码语言:javascript
复制
final Account account = new Account(accountName, intent.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE));

初始化帐户元数据:

代码语言:javascript
复制
String authtoken = //Generated Auth Token ;
String authtokenType = //Auth Token type;
String accountPassword= //Auth password if available;

调用addAccountExplicitly()方法:

代码语言:javascript
复制
mAccountManager.addAccountExplicitly(account, accountPassword, //User data bundle);
mAccountManager.setAuthToken(account, authtokenType, authtoken);

这会很好的走。祝好运!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41313366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档