给定联系人姓名,我需要将其铃声更改为默认铃声,目前我正在做的是
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;
}
}