javaandroidandroid-contactsringtone

Setting custom ringtone to contact


I have an app that among other things is trying to set a specific sound to an individual contact. Everything works, it shows the sound as the ringtone for the contact when you view the contact info but when the contact calls, the default ringtone rings. Can anyone shed any light on what's wrong?

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
        case CONTACT_PICKER_RESULT:
            Uri contactData = data.getData();
            String contactId = contactData.getLastPathSegment();

            Cursor localCursor = managedQuery(contactData, null, null, null, null);
            localCursor.moveToFirst();

            String str1 = localCursor.getString(localCursor.getColumnIndexOrThrow("_id"));
            String str2 = localCursor.getString(localCursor.getColumnIndexOrThrow("display_name"));
            Uri localUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, str1);
            ContentValues localContentValues = new ContentValues();

            localContentValues.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
            localContentValues.put(ContactsContract.Data.CUSTOM_RINGTONE, ringtonePath);
            getContentResolver().update(localUri, localContentValues, null, null);
            Toast.makeText(this, "Ringtone assigned to: " + str2, 0).show();

            break;
        }

Solution

  • In case anybody stumbles across this wondering how to achieve this, I used parts of the ringdroid library to assign a tone to a contact

    https://github.com/google/ringdroid