Setting contact custom ringtone, how?

ε祈祈猫儿з 提交于 2019-11-27 14:13:23
Rotary Heart

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.

Bruno Bieri

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!