Android 联系人:更新、删除、插入

发布于 2024-10-22 15:45:59 字数 574 浏览 2 评论 0原文

我对通讯录的基本操作有疑问,看起来官方示例对我不起作用。其中之一:

ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                      .withValue(Data.RAW_CONTACT_ID, id)
                      .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                      .withValue(Phone.NUMBER, "1-800-GOOG-411")
                      .withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
                      .withValue(Phone.LABEL, "free directory assistance")
                      .build());

这应该添加具有给定 ID 的联系人,但在模拟器中运行此代码后我没有获得任何新联系人。

我将非常感谢任何指导性答案或教程链接。

i have problem with basic operations on the contact book, looks like official examples doesn't work for me. One of them:

ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                      .withValue(Data.RAW_CONTACT_ID, id)
                      .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                      .withValue(Phone.NUMBER, "1-800-GOOG-411")
                      .withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
                      .withValue(Phone.LABEL, "free directory assistance")
                      .build());

This should add a contact with the given id, but I don't get any new contacts after running this code in emulator.

I will really appreciate any guiding answer or link to the tutorials.

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

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

发布评论

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

评论(2

白云不回头 2024-10-29 15:45:59

您应该拥有以下权限:

<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

您应该拥有代码(在您的代码之后):

try {
    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
    e.printStackTrace();
}

这将尝试插入/更新联系人

you should have the following permissions:

<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

you should have the code (after your code):

try {
    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
    e.printStackTrace();
}

this will try to insert / update the contact

赠我空喜 2024-10-29 15:45:59

只需为您的应用程序实现此代码并尝试删除特定联系人列表,

ContentResolver cr = getContentResolver();
                Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?" , new String[] { contactsId(your) }, null);

                if (cur.getCount() > 0) {

                    while (cur.moveToNext()) {

                        try {
                            String lookupKey = cur .getString(cur .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                            Uri uri = Uri.withAppendedPath( ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
                            cr.delete(uri, null, null);
                            setFinish();
                        } catch (Exception e) {
                            e.getStackTrace();
                        }
                    }
                }

Just this code implement for your apps and try out for delete particular contacts list,

ContentResolver cr = getContentResolver();
                Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?" , new String[] { contactsId(your) }, null);

                if (cur.getCount() > 0) {

                    while (cur.moveToNext()) {

                        try {
                            String lookupKey = cur .getString(cur .getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                            Uri uri = Uri.withAppendedPath( ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
                            cr.delete(uri, null, null);
                            setFinish();
                        } catch (Exception e) {
                            e.getStackTrace();
                        }
                    }
                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文