From 将自定义铃声设置为特定的联系号码 https://stackoverflow.com/questions/23404214/android-set-custom-ringtone-to-specific-contact-number
Android 有一个专门的专栏:ContactsContract.CUSTOM_RINGTONE
.
所以,你可以使用ContactsContract.Contacts.getLookupUri
获取您的联系人Uri
,之后几乎剩下的就是打电话ContentResolver.update
.
以下是通过电话号码查找联系人,然后应用自定义铃声的示例:
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;
// The Uri used to look up a contact by phone number
final Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, "012-345-6789");
// The columns used for `Contacts.getLookupUri`
final String[] projection = new String[] {
Contacts._ID, Contacts.LOOKUP_KEY
};
// Build your Cursor
final Cursor data = getContentResolver().query(lookupUri, projection, null, null, null);
data.moveToFirst();
try {
// Get the contact lookup Uri
final long contactId = data.getLong(0);
final String lookupKey = data.getString(1);
final Uri contactUri = Contacts.getLookupUri(contactId, lookupKey);
if (contactUri == null) {
// Invalid arguments
return;
}
// Get the path of ringtone you'd like to use
final String storage = Environment.getExternalStorageDirectory().getPath();
final File file = new File(storage + "/AudioRecorder", "hello.mp4");
final String value = Uri.fromFile(file).toString();
// Apply the custom ringtone
final ContentValues values = new ContentValues(1);
values.put(Contacts.CUSTOM_RINGTONE, value);
getContentResolver().update(contactUri, values, null, null);
} finally {
// Don't forget to close your Cursor
data.close();
}
此外,您还需要添加读取和写入联系人的权限:
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
要对此进行扩展,以及如何根据您的需要对其进行修改,请更改电话号码012-345-6789
在这一行找到您要找的人
// The Uri used to look up a contact by phone number
final Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, "012-345-6789");
并在手机通讯录中设置默认的 CUSTOM_RINGTONE。这里还有另一个类似的选项:设置联系人自定义铃声,如何? https://stackoverflow.com/questions/14230444/setting-contact-custom-ringtone-how