Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

android - Setting contact custom ringtone, how?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...