Add a custom field to a phone number

2019-02-07 12:21发布

问题:

Am building an app that requires a custom contact field ('whitelist', if you were wondering) for every phone number.

However, I only found a way of saving custom data for each contact, with Data.RAW_CONTACT_ID, but not for each phone number. I tried using Phone._id, but I got a java.nullpointerexception error.

This is the code I have now:

try{ //phoneId = get Phone._ID from cursor
  ContentValues values = new ContentValues();                           
  values.put(Data.DATA1, "yes");
  int state = getContentResolver().update(Phone.CONTENT_URI, values, Phone._ID + " = " 
              + phoneId + " AND " +  Data.MIMETYPE + "='" + MIMETYPE_WHITELIST_CONTACT+"'", null);
         if (state == 0) {
             values.put(Phone._ID, phoneId);
             values.put(Data.DATA1, "yes");
             values.put(Data.MIMETYPE, MIMETYPE_WHITELIST_CONTACT);
                            getContentResolver().insert(Data.CONTENT_URI, values);
        }  
}catch (Exception e) {
    Toast.makeText(ChooseContactsActivity.this,  e.toString(), Toast.LENGTH_LONG).show();
}

Does anyone have any idea what needs to be changed so it can save a custom field for the phone number, not the contact?

回答1:

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.



回答2:

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);


回答3:

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);


回答4:

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