Setting contact custom ringtone, how?
Asked Answered
M

2

5

I know how to change phone ringtone, also how to get contacts, but how can I set a ringtone for a specific contact?

So how do I use the method: ContactsContract.Contacts.CUSTOM_RINGTONE?

I have tried it like this:

Uri contactUri = ContactsContract.Contacts.CONTENT_URI;
String[] PROJECTION = new String[] {
    ContactsContract.Contacts._ID,
    ContactsContract.Contacts.DISPLAY_NAME,
    ContactsContract.Contacts.HAS_PHONE_NUMBER,
};

String SELECTION = ContactsContract.Contacts.HAS_PHONE_NUMBER + "='1'";
Cursor contacts = managedQuery(contactUri, PROJECTION, SELECTION, null, null );

while (contacts.moveToNext())
{
    String Name=contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
}

String str1 = contacts.getString(contacts.getColumnIndexOrThrow("_id"));

Uri localUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, str1);
ContentValues localContentValues = new ContentValues();
localContentValues.put(ContactsContract.Contacts.CUSTOM_RINGTONE, 
    f.getAbsolutePath()+"/Adveture.ogg");
    MainActivity.this.getContentResolver().update(localUri, localContentValues, null, null);

But it's not working.

Matejka answered 9/1, 2013 at 7:44 Comment(5)
Can you add how you get the contacts?Circassian
Did you try that code it should give you the contacts.Matejka
What I mean is how do you let the user choose a contact and use the chosen contact as source for the above code. I found this:Intent intent = new Intent(Intent.ACTION_PICK); intent.setType(ContactsContract.Contacts.CONTENT_TYPE); startActivityForResult(intent, CONTACT); and in the onActivityResult method I used Uri contactData = data.getData(); to get the chosen contact but get a cursorIndexOutofBounds exceptionCircassian
Oooo I see, I didn't use that. I made a ListView activity where it shows all the contact names. Then the user select the name and check every contact with the name selected.Matejka
Ok, I got it. I had to change the line localCursor.move(Integer.valueOf(contactId)); to localCursor.moveToFirst(); because I had already one contact and it was not necessary to move the cursor at a certain position. I'll post my complete answer below.Circassian
M
8

I found out how it works. Below you can see the fixed code code:

    Uri contactData = ContactsContract.Contacts.CONTENT_URI;
    String contactId = contactData.getLastPathSegment();

    Cursor localCursor = managedQuery(contactData, PROJECTION, null, null, null);
    localCursor.move(120/*CONTACT ID NUMBER*/);

    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, f.getAbsolutePath()+"/Adventure.ogg");
    getContentResolver().update(localUri, localContentValues, null, null);
    Toast.makeText(this, "Ringtone assigned to: " + str2, 0).show();

Just change the contact id number to the id of the contact you want to change.

Matejka answered 9/1, 2013 at 12:3 Comment(8)
I keep having this error : android.database.sqlite.SQLite Exception: no such column: custom ringtone (code 1): , while compiling: update data set custom_ringtone=?Preheat
It doesn't change ringtone? and doesn't even throw an exception! any idea?Contempt
@Contempt Did you modify anything? This code should work without issues.Matejka
@RotaryHear No I've tried with different contacts but it never changes the default ringtone! don't know why, though I'm on 4.4.2 kitkatContempt
and what's with ""/Adventure.ogg"? because I'm simply using exact file path /sdcard/rintone.mp3Contempt
@Contempt /Adventure.ogg is the file name. I don't really know why it's not workong eith your device. You should create a new question and paste your codeMatejka
What is "PROJECTION" here?Bankable
@RotaryHeart, it gives me error on "f.getAbsolutePath(), . How to get path please guideCopeland
C
2

To open the default contact search of android use this code:

// put that constant in your class
static public final int CONTACT_CHOOSER_ACTIVITY_CODE = 73729;

// start contact search activity within any method you like
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, CONTACT_CHOOSER_ACTIVITY_CODE);

In the onActivityResult method you can use this code (similar to Rotary Heart's code) to set the contacts ringtone:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case (CONTACT_CHOOSER_ACTIVITY_CODE) :
            if (resultCode == Activity.RESULT_OK) {

                try{
                    Uri contactData = data.getData();
                    String contactId = contactData.getLastPathSegment();
                    String[] PROJECTION = new String[] {
                            ContactsContract.Contacts._ID,
                            ContactsContract.Contacts.DISPLAY_NAME,
                            ContactsContract.Contacts.HAS_PHONE_NUMBER,
                    };
                    Cursor localCursor =  getContentResolver().query(contactData, PROJECTION, null, null, null);
                    localCursor.moveToFirst();
                    //--> use moveToFirst instead of this:  localCursor.move(Integer.valueOf(contactId)); /*CONTACT ID NUMBER*/

                    String contactID = localCursor.getString(localCursor.getColumnIndexOrThrow("_id"));
                    String contactDisplayName = localCursor.getString(localCursor.getColumnIndexOrThrow("display_name"));

                    Uri localUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactID);
                    localCursor.close();
                    ContentValues localContentValues = new ContentValues();

                    localContentValues.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
                    localContentValues.put(ContactsContract.Data.CUSTOM_RINGTONE, f.getAbsolutePath()+"/Adventure.ogg");
                    getContentResolver().update(localUri, localContentValues, null, null);

                    Toast.makeText(this, "Ringtone assigned to: " + contactDisplayName, Toast.LENGTH_LONG).show();

                } catch(Exception ex){
                    Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
            break;
    }

}

Note: you still have to set the f variable (in code f.getAbsolutePath()+"/Adventure.ogg") to your file (ringtone) you like to set.

This code was tested with android 2.3. Maybe there are changes necessary for higher versions.

Circassian answered 3/8, 2013 at 6:27 Comment(3)
This code was tested with android 2.3. Maybe there are changes necessary for higher versions.Circassian
Bro you miss to show Toast. ( .show() ) and thats not so successful toast message..!!Gain
@Gain you're free to edit/improve my answer. Maybe you refer to a never version of Android.Circassian

© 2022 - 2024 — McMap. All rights reserved.