4

I know how to create contact with name,mobile number,email id for native app programmatically using How to add new contacts in android. But I do not know how to create contact with ringtone. Please help me. Thanks in advance

I got the solution to add the ringtone after adding the contacts into native app:

String select = "(" + ContactsContract.Contacts.DISPLAY_NAME + " == \"" +first_name+ "\" )"; 

Cursor c1 = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, 
CONTACTS_SUMMARY_PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

startManagingCursor(c1);

int id=0;

if (c1.moveToNext()) {

id = new Integer(c1.getString(0)).intValue();

Toast.makeText(getApplicationContext(), "CONTACT ID: "+id+"", Toast.LENGTH_LONG).show(); 

} 

ContentResolver cr = getContentResolver(); 

cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);

ContentValues values=new ContentValues(); values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, newgroup_ringtone); 
cr.update(ContactsContract.Contacts.CONTENT_URI, values, Contacts._ID + "=" + id, null);
Community
  • 1
  • 1
AndroidRaji
  • 907
  • 14
  • 26
  • How to Insert new contacts with Ringtone? please help me i need very urgent – AndroidRaji Sep 25 '12 at 05:58
  • Following codes are not working for me to add contacts with ringtone.It shows insert failed exception try { if(ringtonepath != null) { ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) .withValue(ContactsContract.CommonDataKinds.Phone.CUSTOM_RINGTONE, ringtonepath) .build() ); } } – AndroidRaji Sep 25 '12 at 06:00

1 Answers1

1

Why not just add the contact first then retrieve that contact and update the contact with the ringtone with code like this:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Contacts.CONTENT_URI)
        .withSelection(ContactsContract.Contacts._ID + " = ?", new String[] {id})
        .withValue(ContactsContract.Contacts.STARRED, starred)
        .withValue(ContactsContract.Contacts.SEND_TO_VOICEMAIL, sendToVoicemail)
        .withValue(ContactsContract.Contacts.CUSTOM_RINGTONE, ringtone)
        .build());

try {
    resolver.applyBatch(ContactsContract.AUTHORITY, ops);
}
Michael Wildermuth
  • 5,762
  • 3
  • 29
  • 48