如何为联系人设置默认铃声 - Android以编程方式?

时间:2013-11-14 14:40:17

标签: android android-contacts

根据联系人姓名,我需要将其铃声更改为默认铃声, 目前我正在做的是

1)迭代所有联系人

2)如果找到了所需的联系人,请使用新的铃声(内部存储中存在的xperia.mp3)准备内容值并更新。

但代码显示没有效果?

我是Android的新手,我需要帮助才能做到这一点,也阅读其他答案,但他们没有太大的帮助。

String whereName = ContactsContract.Data.MIMETYPE + " = ?";
String[] whereNameParams = new String[]{ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
// Get cursor to all the names
Cursor nameCur = getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
    String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
    // if name equals name to check
    if(display.equals(tocheck)){ // to check contains the name to check.
        Uri contactData = ContactsContract.Contacts.CONTENT_URI;
        String contactId = nameCur.getString(nameCur.getColumnIndexOrThrow("_id"));
        String[] PROJECTION = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.HAS_PHONE_NUMBER,
            };

        Cursor localCursor = getActivity().managedQuery(contactData, PROJECTION, null, null, null);
        localCursor.move(Integer.valueOf(contactId)/*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();
        File newSoundFile = new File(Environment.getExternalStorageDirectory() + File.separator + "xperia.mp3"); // save new ringtone.
        localContentValues.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
        localContentValues.put(ContactsContract.Data.CUSTOM_RINGTONE,newSoundFile.getAbsolutePath()); 
        getActivity().getContentResolver().update(localUri, localContentValues, null, null);
        nameCur.close();
        break;
        }

}

0 个答案:

没有答案