Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename an account on Android (AccountManager)

I'm changing the name of a published app.

Is there a quick and safe way to change the account name created via AccountManager.addAccountExplicitly so that existing info will remain intact for existing users.

If not, how can I go about changing the account name manually while preserving all the data?

I'll post an answer of my naive approach of copying everything then deleting the old, but I'm sure someone will come up with a better one (or spot some bugs in my method).

like image 664
marmor Avatar asked Oct 25 '12 16:10

marmor


1 Answers

API v21 added a renameAccount() method to the AccountManager, if that helps.

From the docs:

This is equivalent to removing the existing account and adding a new renamed account with the old account's user data.

That means for backward compatibility, you would have to manually remove the account and run through the same procedure as creating a new one (AccountManager.addAccountExplicitly() and AccountManager.setUserData()) afterwards.

Edit: If you want to update your contacts afterwards to display the correct account name, try this (untested) code:

ContentValues contentValues = new ContentValues();
contentValues.put(ContactsContract.RawContacts.ACCOUNT_NAME, "new account name");
getContext().getContentResolver().update(ContactsContract.RawContacts.CONTENT_URI,
        contentValues,
        ContactsContract.RawContacts.ACCOUNT_TYPE + " = ? AND " + ContactsContract.RawContacts.ACCOUNT_NAME + " = ?",
        new String[]{"your account type", "old account name"});
like image 166
saschoar Avatar answered Oct 18 '22 16:10

saschoar