如何为 API 级别 21 中 android 中的特定联系人设置铃声?
how can I set a ringtone for a specific contact in android in API level 21?
我知道如何更改 phone 铃声,也知道如何获取联系人,但如何为特定联系人设置铃声?
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, Cob.selectedPath.get(0) /*DIRECT PATH TO MP3 File*/);
getContentResolver().update(localUri, localContentValues, null, null);
Toast.makeText(this, "Ringtone assigned to: " + str2, 0).show();
Android has a special column for this:
ContactsContract.CUSTOM_RINGTONE
.
So, you could use ContactsContract.Contacts.getLookupUri
to get your
contact's Uri, after that pretty much all that's left is to call
ContentResolver.update
.
最初来自这个 Whosebug answer。
Cob.selectedPath.get(0) /*DIRECT PATH TO MP3 File*/);
不,"direct path to an MP3 file" 行不通。您必须使用 android api 来获取路径(就像我已经链接到的 Whosebug 答案中所做的那样)。希望你没有这样做,尽管你的评论说了什么,但我不能肯定地说,因为我不知道你的 Cob.selectedPath.get(0)
.
里有什么
我知道如何更改 phone 铃声,也知道如何获取联系人,但如何为特定联系人设置铃声?
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, Cob.selectedPath.get(0) /*DIRECT PATH TO MP3 File*/);
getContentResolver().update(localUri, localContentValues, null, null);
Toast.makeText(this, "Ringtone assigned to: " + str2, 0).show();
Android has a special column for this:
ContactsContract.CUSTOM_RINGTONE
.So, you could use
ContactsContract.Contacts.getLookupUri
to get your contact's Uri, after that pretty much all that's left is to callContentResolver.update
.
最初来自这个 Whosebug answer。
Cob.selectedPath.get(0) /*DIRECT PATH TO MP3 File*/);
不,"direct path to an MP3 file" 行不通。您必须使用 android api 来获取路径(就像我已经链接到的 Whosebug 答案中所做的那样)。希望你没有这样做,尽管你的评论说了什么,但我不能肯定地说,因为我不知道你的 Cob.selectedPath.get(0)
.