将选定的帐户传递给其他活动

Pass Selected Account to other activities

在我的 android 应用程序中,我使用客户经理。如果有多个帐户,我要求用户使用 accountManager.newChooseAccountIntent select 一个帐户。我如何才能在下次登录表单和其他活动中记住这个 selection?

给你:

1.启动向用户显示帐户选择对话框的意图-

public static final int CHOOSE_ACCOUNT_REQUEST_CODE = 0;
private String mEmail; 

Intent intent = AccountManager.newChooseAccountIntent(null, null,
    new String[] { acc_type }, true, null, null, null, null);
startActivityForResult(intent, CHOOSE_ACCOUNT_REQUEST_CODE);

2。在 onActivityResult 里面获取结果 -

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent   
data) {
if (requestCode == REQUEST_CODE_PICK_ACCOUNT) {

    if (resultCode == RESULT_OK) {

        mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);

    } else if (resultCode == RESULT_CANCELED) {
        // The account picker dialog closed without selecting an account.
        // Notify users that they must pick an account to proceed.

    }
}

}

3。然后您可以将 mEmail 存储在共享首选项中以在 申请:

SharedPreferences sharedPreferences =    
context.getSharedPreferences("app_pref", MODE_PRIVATE);

editor = sharedPreferences.edit();

editor.putBoolean("Email", mEmail);

editor.commit();

4.要从共享首选项访问存储的电子邮件:

String email = sharedPreferences.getString("Email", "Use a default value");