使用权限android.permission.MANAGE_ACCOUNTS恢复额外的用户数据

时间:2014-05-21 18:26:25

标签: android accountmanager

当我尝试在AccountManager中明确添加帐户时,我遇到了问题。

问题

包含用户数据的捆绑包保存在/data/system/users/0/accounts.db中,但未被保存恢复。

代码

// 1st step) Adding account with extra user data
String email = "betorcs@mail.com";

final Account a = new Account(email, getString(R.string.accountType));
final Bundle userData = new Bundle();
userData.putString("firstName", "Roberto");

final AccountManager am = AccountManager.get(context);
boolean saved = am.addAccountExplicitly(a, null, userData); // It's always returning true

// 2nd step) Restoring account and extra user data
Account[] as = am.getAccountsByType(getString(R.string.accountType)); // Assuming as.length == 1
String firstName = am.getUserData(as[0], "firstName"); // It's always null

权限

<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />

BUT

如果我从清单中删除权限android.permission.MANAGE_ACCOUNTS,一切正常。

有人能看出它的错误吗?或者为什么会出现这种情况?

由于

1 个答案:

答案 0 :(得分:2)

我替换了

// 1st step) Adding account with extra user data
String email = "betorcs@mail.com";

final Account a = new Account(email, getString(R.string.accountType));
final Bundle userData = new Bundle();
userData.putString("firstName", "Roberto");

final AccountManager am = AccountManager.get(context);
boolean saved = am.addAccountExplicitly(a, null, userData);

通过

// 1st step) Adding account with extra user data
String email = "betorcs@mail.com";

final Account a = new Account(email, getString(R.string.accountType));
final AccountManager am = AccountManager.get(context);
boolean saved = am.addAccountExplicitly(a, null, null);
if (saved) {
     am.setUserData(a, "firstName", "Roberto");
}

现在它正在工作,我还不明白,但工作了!

经过测试

  • i9100
  • i9300
  • Nexus 4