如何在 Android 中以编程方式向联系人添加电话号码?

发布于 2024-12-12 03:11:06 字数 1918 浏览 0 评论 0原文

我有一个可以很好地添加和删除联系人的应用程序。如果现有联系人中有现有值,则可以对其进行修改。但我似乎无法将新值插入现有联系人中。例如,如果家庭电话号码有现有值,但工作电话号码没有,我尝试使用填充来添加值(cintact idValueworkNumber 被传入):

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValue(ContactsContract.Data.CONTACT_ID, idValue)
            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, workNumber)
            .build());
    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

但是我得到一个 NullPointerException:

java.lang.NullPointerException
    at com.android.providers.contacts.ContactsProvider2.insertData(ContactsProvider2.java:2604)
    at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:2452)
    at com.android.providers.contacts.SQLiteContentProvider.insert(SQLiteContentProvider.java:106)
    at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:2256)
    at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:214)
    at com.android.providers.contacts.SQLiteContentProvider.applyBatch(SQLiteContentProvider.java:216)
    at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:2290)
    at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:217)
    at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:194)
    at android.os.Binder.execTransact(Binder.java:336)
    at dalvik.system.NativeStart.run(Native Method)

有人可以告诉我我做错了什么吗?

I have an app that can add and delete contacts just fine. And if there is an existing value in an existing contact, it can be modified just fine. But I can't seem to be able to insert new values into existing contacts. For example, if there is an existing value for a home phone number, but not for the work phone number, I tried using the fillowing to add the value (cintact idValue and workNumber are passed in):

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValue(ContactsContract.Data.CONTACT_ID, idValue)
            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, workNumber)
            .build());
    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

But I get a NullPointerException:

java.lang.NullPointerException
    at com.android.providers.contacts.ContactsProvider2.insertData(ContactsProvider2.java:2604)
    at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:2452)
    at com.android.providers.contacts.SQLiteContentProvider.insert(SQLiteContentProvider.java:106)
    at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:2256)
    at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:214)
    at com.android.providers.contacts.SQLiteContentProvider.applyBatch(SQLiteContentProvider.java:216)
    at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:2290)
    at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:217)
    at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:194)
    at android.os.Binder.execTransact(Binder.java:336)
    at dalvik.system.NativeStart.run(Native Method)

Can someone please tell me what I'm doing wrong?

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

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

发布评论

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

评论(2

一绘本一梦想 2024-12-19 03:11:06

您缺少 withValue(Phone.TYPE, Phone.TYPE_WORK)

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValue(ContactsContract.Data.RAW_CONTACT_ID, idValue)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, workNumber).
withValue(Phone.TYPE, Phone.TYPE_WORK)
        .build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

编辑

您是正确的,我检查了 data 表的架构,它是遵循它仅使用原始联系人 ID,而不使用联系人 ID

CREATE TABLE data (_id INTEGER PRIMARY KEY AUTOINCREMENT,
package_id INTEGER REFERENCES package(_id),
mimetype_id INTEGER REFERENCES mimetype(_id) NOT NULL,
raw_contact_id INTEGER REFERENCES raw_contacts(_id) NOT NULL,
is_primary INTEGER NOT NULL DEFAULT 0,
is_super_primary INTEGER NOT NULL DEFAULT 0,
data_version INTEGER NOT NULL DEFAULT 0,
data1 TEXT,
data2 TEXT,
data3 TEXT,
data4 TEXT,
data5 TEXT,
data6 TEXT,
data7 TEXT,
data8 TEXT,
data9 TEXT,
data10 TEXT,
data11 TEXT,
data12 TEXT,
data13 TEXT,
data14 TEXT,
data15 TEXT,
data_sync1 TEXT, 
data_sync2 TEXT, 
data_sync3 TEXT, 
data_sync4 TEXT );

You are missing withValue(Phone.TYPE, Phone.TYPE_WORK)

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
        .withValue(ContactsContract.Data.RAW_CONTACT_ID, idValue)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, workNumber).
withValue(Phone.TYPE, Phone.TYPE_WORK)
        .build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

Edits

You are correct I checked the schema for the data table and it is as follow it is only using raw contacts id and not contact id

CREATE TABLE data (_id INTEGER PRIMARY KEY AUTOINCREMENT,
package_id INTEGER REFERENCES package(_id),
mimetype_id INTEGER REFERENCES mimetype(_id) NOT NULL,
raw_contact_id INTEGER REFERENCES raw_contacts(_id) NOT NULL,
is_primary INTEGER NOT NULL DEFAULT 0,
is_super_primary INTEGER NOT NULL DEFAULT 0,
data_version INTEGER NOT NULL DEFAULT 0,
data1 TEXT,
data2 TEXT,
data3 TEXT,
data4 TEXT,
data5 TEXT,
data6 TEXT,
data7 TEXT,
data8 TEXT,
data9 TEXT,
data10 TEXT,
data11 TEXT,
data12 TEXT,
data13 TEXT,
data14 TEXT,
data15 TEXT,
data_sync1 TEXT, 
data_sync2 TEXT, 
data_sync3 TEXT, 
data_sync4 TEXT );
£烟消云散 2024-12-19 03:11:06

您收到错误是因为 Android 不允许您直接将联系人添加到您的联系人表中。相反,您需要修改或添加原始联系人,Android 会自动为您创建联系人。

You were getting error because Android doesn't give you access to add a contact directly to you Contact table. Rather you need to modify or add a raw Contact and Android will automatically create a Contact for you.

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