来自服务的Android AccountManager addAccount

时间:2016-12-24 12:42:42

标签: android accountmanager

我在服务中使用了一个客户经理帐户(扩展了FirebaseInstanceIdService)。如果没有有效帐户,那么我使用accountManager.addAccount。

添加帐户

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

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
    }
}

1 个答案:

答案 0 :(得分:0)

addAccount()方法要求Activity实例调用默认的Authenticator Activity。

它们是AccountManager类中另一个名为addAccountExplicitly()的方法。 这是文档:

/**
     * 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); 

<强>用法:

创建帐户实例:

final Account account = new Account(accountName, intent.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE));

初始化帐户元数据:

String authtoken = //Generated Auth Token ;
String authtokenType = //Auth Token type;
String accountPassword= //Auth password if available;

调用addAccountExplicitly()方法:

mAccountManager.addAccountExplicitly(account, accountPassword, //User data bundle);
mAccountManager.setAuthToken(account, authtokenType, authtoken);

这将是好事。祝你好运!