Android中选择账户

当系统中存在多个账户的时候,需要让用户手动选择或添加账户. Android 已经提供此功能. 但因为它是AccountManager的静态方法,所以没有注意到,特此备注一下:

以下是SDK参考文档中的说明:

public static Intent newChooseAccountIntent (Account selectedAccount, ArrayList <Account > allowableAccounts, String[] allowableAccountTypes, boolean alwaysPromptForAccount, String descriptionOverrideText, String addAccountAuthTokenType, String[] addAccountRequiredFeatures, Bundle addAccountOptions)

Since: API Level 14

Returns an intent to an Activity that prompts the user to choose from a list of accounts. The caller will then typically start the activity by calling startActivityWithResult(intent, ...); .

On success the activity returns a Bundle with the account name and type specified using keys KEY_ACCOUNT_NAME and KEY_ACCOUNT_TYPE .

The most common case is to call this with one account type, e.g.:

newChooseAccountsIntent(null, null, new String[]{"com.google"}, false, null,
 null, null, null);
 Parameters
selectedAccount allowableAccounts allowableAccountTypes alwaysPromptForAccount descriptionOverrideText addAccountAuthTokenType addAccountRequiredFeatures addAccountOptions
if specified, indicates that the Account is the currently selected one, according to the caller's definition of selected.
an optional ArrayList of accounts that are allowed to be shown. If not specified then this field will not limit the displayed accounts.
an optional string array of account types. These are used both to filter the shown accounts and to filter the list of account types that are shown when adding an account.
if set the account chooser screen is always shown, otherwise it is only shown when there is more than one account from which to choose
if non-null this string is used as the description in the accounts chooser screen rather than the default
this string is passed as the addAccount(String, String, String[], Bundle, Activity, AccountManagerCallback, Handler) authTokenType parameter
this string array is passed as the addAccount(String, String, String[], Bundle, Activity, AccountManagerCallback, Handler) requiredFeatures parameter
This Bundle is passed as the addAccount(String, String, String[], Bundle, Activity, AccountManagerCallback, Handler) options parameter
Returns
  • an Intent that can be used to launch the ChooseAccount activity flow.

代码:

                Intent intent = AccountManager.newChooseAccountIntent(
                        (Account) null, (ArrayList<Account>) null,
                        new String[] { ITRGlsAuthorizer.ACCOUNT_TYPE }, false,
                        null, ITRGlsAuthorizer.PICASA_AUTH_TOKEN_TYPE,
                        (String[]) null, (Bundle) null);
                startActivityForResult(intent, 0x20);
 

你可能感兴趣的:(android)