Rename your account on Android (AccountManager)

I am changing the name of the published application.

Is there a quick and safe way to change the account name created with AccountManager.addAccountExplicitly so that existing information remains intact for existing users.

If not, how can I manually change the account name while saving all the data?

I will write the answer of a naive approach to copying everything, and then delete the old one, but I'm sure someone will come up with the best one (or detect some errors in my method).

+4
source share
2 answers

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

From the docs:

This is equivalent to deleting an existing account and adding a new renamed account with old user account information.

This means that for backward compatibility you will have to manually delete the account and perform the same procedure as after creating a new one ( AccountManager.addAccountExplicitly() and AccountManager.setUserData() ).

Edit: If after that you want to update your contacts to display the correct account name, try this (unverified) 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"}); 
+5
source

The naive approach is to iterate over all the records, copy them one by one and delete all old things ...

I am really afraid that this method may fail with real-world users.

 private void naiveRename(ContentResolver resolver) { ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>(); Cursor cur = resolver.query(RawContacts.CONTENT_URI, null, RawContacts.ACCOUNT_NAME + "='" + "OLD NAME" + "'", null, null); if (cur != null) { // copy all data while (cur.moveToNext()) { Uri curUri = RawContacts.CONTENT_URI.buildUpon() .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true") .build(); ContentProviderOperation.Builder builder = ContentProviderOperation .newInsert(curUri); for (int i = 0; i < cur.getColumnCount(); i++) { String colName = cur.getColumnName(i); if (RawContacts._ID.equals(colName) || RawContacts.VERSION.equals(colName) || RawContacts.CONTACT_ID.equals(colName)) { // Skip - read only } else if (RawContacts.ACCOUNT_NAME.equals(colName)) { builder.withValue(RawContacts.ACCOUNT_NAME, "NEW NAME"); } else { builder.withValue(colName, cur.getString(i)); } } operationList.add(builder.build()); } // delete all old data ContentProviderOperation.Builder builder = ContentProviderOperation .newDelete(RawContacts.CONTENT_URI); builder.withSelection(RawContacts.ACCOUNT_NAME + "='" + "OLD NAME" + "'", null); try { resolver.applyBatch(ContactsContract.AUTHORITY, operationList); } catch (RemoteException e) { // PANIC! } catch (OperationApplicationException e) { // OMG! WHAT TO DO?! } } else { // LORDI! } } 
0
source

Source: https://habr.com/ru/post/1442111/


All Articles