简体   繁体   English

在Android上重命名帐户(AccountManager)

[英]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. 是否有快速安全的方法来更改通过AccountManager.addAccountExplicitly明确创建的帐户名称,以便现有用户的现有信息保持不变。

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. 如果有帮助,API v21将一个renameAccount()方法添加到AccountManager

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. 这意味着为了向后兼容,您必须手动删除帐户,然后运行与创建新帐户( AccountManager.addAccountExplicitly()AccountManager.setUserData() )相同的过程。

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!
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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