批量添加多个联系人

发布于 2024-11-30 13:56:50 字数 1162 浏览 6 评论 0原文

我完全能够使用以下代码一一添加联系人:

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());

    ops.add(ContentProviderOperation
            .newInsert(Data.CONTENT_URI)
            .withValueBackReference(Data.RAW_CONTACT_ID, 0)
            .withValue(Data.MIMETYPE,
                    CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                    .withValue(StructuredName.GIVEN_NAME, "Hello")
                    .withValue(StructuredName.FAMILY_NAME, "World").build());

    try {
        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (OperationApplicationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

但是,当我尝试一一添加大约 500 个联系人时 - 需要几分钟,这对于我的应用程序来说太长了。有没有更快的方法来添加多个联系人?

I am perfectly able to add contacts one by one with following code:

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());

    ops.add(ContentProviderOperation
            .newInsert(Data.CONTENT_URI)
            .withValueBackReference(Data.RAW_CONTACT_ID, 0)
            .withValue(Data.MIMETYPE,
                    CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                    .withValue(StructuredName.GIVEN_NAME, "Hello")
                    .withValue(StructuredName.FAMILY_NAME, "World").build());

    try {
        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (OperationApplicationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

However, when I try to add about 500 contacts one by one - it takes few minutes, which is too long for my app. Is the any faster way to add several contacts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请帮我爱他 2024-12-07 13:56:50

为什么不将数组列表设置为可以从任何活动访问的全局数组列表,我不会在捆绑包中插入那么多内容,因为当您这样做时会发生更多事情,它只是为了传递少量信息。我会这样做,确保也在清单中调用它..

public class MyStates extends Application {

    private  ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    public ArrayList getList() {
    return this.blueToothAdapter;
    }

    public void setList(ArrayList<ContentProviderOperation> o) {
        this.ops= o;
    }

Why not make the arraylist a global that can be accessed from any activity I wouldn't insert that much into a Bundle as there more going on when you do, it was only meant to pass small amounts info. I would do it like this, making sure to call this in the manifest too..

public class MyStates extends Application {

    private  ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    public ArrayList getList() {
    return this.blueToothAdapter;
    }

    public void setList(ArrayList<ContentProviderOperation> o) {
        this.ops= o;
    }
哆啦不做梦 2024-12-07 13:56:50

您可以使用与在单个批量操作中添加多个联系人相同的功能进行少量修改。

您最多可以向单个批量操作添加 500 个操作,您可以继续在 Data Uri 操作中包含反向引用以及 raw_contacts 插入操作的相应索引。

You can use the same function you are using to add multiple contacts in a single batch operation by making small modifications.

You can add upto 500 operations to a single batch operation, you can keep on including the back-reference in Data Uri operation with the corresponding index of the raw_contacts insert operation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文