批驳的AccountManager多个帐户 [英] Disallow multiple accounts in AccountManager

查看:188
本文介绍了批驳的AccountManager多个帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了我的AccountManager身份验证和服务,整个交易,这一切似乎是工作的罚款。

I've implemented my AccountManager authenticator and the service and the whole deal and it all seems to be working fine.

有,然而,一个小问题:我只想要一个帐户我的应用程序的客户经理存在,但不能完全似乎找到一种方法来限制​​这种

There is, however, one little problem: I only want a single account for my app to exist in account manager, but can't quite seem to find a way to limit this.

是我唯一的解决方案添加新帐户之前做搜索并删除当前帐户(按类型)?

Is my only solution to do a search and delete the current accounts (by type) before adding the new account?

我目前的code:

private void removeAccounts()
{
    Account [] accounts = mAcctMgr.getAccountsByType (mAccountType);

    if (accounts.length == 0) return;

    final Handler handler = new Handler (); 

    AccountManagerCallback<Boolean> callback = new AccountManagerCallback<Boolean>()
    {
        @Override
        public void run(AccountManagerFuture<Boolean> arg0)
        {
           // nada
        }
    };

    for (Account a : accounts) {
        mAcctMgr.removeAccount (a, callback, handler);
    }
}

我不以任何方式把这种优雅的解决方案,不过目前似乎是工作的唯一的事。

I don't by any means call this an elegant solution, but at the moment seems to be the only thing that works.

推荐答案

href="http://developer.android.com/reference/android/accounts/AccountManager.html#addAccount%28java.lang.String,%20java.lang.String,%20java.lang.String%5B%5D,%20android.os.Bundle,%20android.app.Activity,%20android.accounts.AccountManagerCallback%3Candroid.os.Bundle%3E,%20android.os.Handler%29">javadocs为 addAccount() ,如果​​在创建帐户时出现错误情况,你应该返回一个包含包的 KEY_ERROR_ $ C $ ç KEY_ERROR_MESSAGE 参数,

per the javadocs for addAccount(), if an error condition occurs when creating the account, you should return a bundle that contains the KEY_ERROR_CODE and KEY_ERROR_MESSAGE parameters,

    if (accountExists) {
        final Bundle result = new Bundle();
        result.putInt(AccountManager.KEY_ERROR_CODE, ERROR_CODE_ONE_ACCOUNT_ALLOWED);
        result.putString(AccountManager.KEY_ERROR_MESSAGE, context.getString(R.string.one_account_allowed));

        handler.post(new Runnable() {

            @Override
            public void run() {
                RepeatSafeToast.show(context, R.string.one_account_allowed);
            }
        });
        return result;
    }

返回空的的平均故障,就意味着其结果将通过响应参数到 addAccount()办法沟通。

returning null does not mean failure, it means that the result will be communicated through the response parameter to the addAccount() method.

这篇关于批驳的AccountManager多个帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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