Add a custom field to a phone number

我的梦境 提交于 2019-11-30 10:29:18

Try this code for Add custom label number. This is working code in my app...

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

if (CustomLabelNo != null) {
            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, id)
                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, CustomLabelNo)
                    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM)
                    .withValue(ContactsContract.CommonDataKinds.Phone.LABEL, "mayurbhola_newCustomTest").build());
        }

description : CustomLabelNo : any number which you want to add in your contact. ops : this is ArrayList.

getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

[Ref Link for know about getContentResolver] : http://developer.android.com/guide/topics/providers/content-providers.html this code for Add contact......

you can add other type of number and display name as per requirement.

All right, I figured it out. Maybe there is a better solution, but this works for me:

    values.put(Data.RAW_CONTACT_ID, contactId);
    values.put(Data.DATA1, phoneId);
    values.put(Data.DATA2, "1");
    values.put(Data.DATA5, phoneNum);
    values.put(Data.MIMETYPE, MIMETYPE_WHITELIST_CONTACT);
getContentResolver().insert(Data.CONTENT_URI, values);

When I query, I just have to add the phoneId to get the result I need:

getContentResolver().query(Data.CONTENT_URI, {Data.DATA1, Data.DATA2},
 Data.RAW_CONTACT_ID + " = " + contactId +  " AND "  + 
    Data.DATA1 + " = " + phoneId + " AND "  +
            Data.MIMETYPE + "='" + MIMETYPE_WHITELIST_CONTACT+"'", null, null);
This is how you do it if you want your custom label:

    Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
    intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
    intent.putExtra(ContactsContract.Intents.Insert.NAME, name);

    ArrayList<ContentValues> data = new ArrayList<>();                        
    ContentValues phonesRow = new ContentValues();

    phonesRow.put(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
                                            phonesRow.put(ContactsContract.CommonDataKinds.Phone.NUMBER,number);
    phonesRow.put(ContactsContract.CommonDataKinds.Phone.LABEL,type);                              phonesRow.put(ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM);
data.add(phonesRow);

As the class Contact is deprecated and has been superseded by ContactsContract, you 'd better try the new one. The newer APIs allow access multiple accounts and support aggregation of similar contacts.

For the new Phone class, it has defined three columns.

  • String NUMBER DATA1
  • int TYPE DATA2
  • String LABEL DATA3

I think you can use the Label column of ContactsContract.CommonDataKinds.Phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!