设置联系人自定义铃声,怎么样?

时间:2013-01-09 07:44:03

标签: android mp3 contacts ogg ringtone

我知道如何更改手机铃声,以及如何获取联系人,但如何为特定联系人设置铃声?

那我该如何使用这个方法:ContactsContract.Contacts.CUSTOM_RINGTONE

我试过这样的话:

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);

但它不起作用。

2 个答案:

答案 0 :(得分:8)

我发现它是如何工作的。您可以在下面看到固定的代码:

    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();

只需将联系人ID号更改为您要更改的联系人的ID。

答案 1 :(得分:2)

要打开android的默认联系人搜索,请使用以下代码:

// 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);

在onActivityResult方法中,您可以使用此代码(类似于Rotary Heart的代码)来设置联系人铃声:

@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;
    }

}

注意:您仍然需要将f变量(在代码f.getAbsolutePath()+“/ Adventure.ogg”中)设置为您要设置的文件(铃声)。

此代码已使用android 2.3进行测试。也许更高版本需要进行更改。