简体   繁体   中英

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).

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"});

A naive approach of going over all the records, copying them one by one, and deleting all the old stuff...

I'm really afraid this method might fail on 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!
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM