在我的Android应用程序中,我使用客户经理。如果有多个帐户,我会要求用户使用accountManager.newChooseAccountIntent选择一个帐户。如何在登录表单和其他活动中记住下一次选择?
答案 0 :(得分:2)
你走了:
<强> 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");