如何在安卓手机上为单个联系人设置个性化铃声?

6
如何为指定联系人设置铃声?
我已经找到了一种方法来设置适用于所有联系人的默认铃声,但这并不是我的目标。
我想要一个应用程序有一个按钮("将铃声应用于联系人"),当点击该按钮时,会启动一个activityForResult,显示手机上所有联系人的列表。当选择一个联系人后,联系人activity将关闭并返回一个URI给该联系人。然后该应用需要将所选铃声应用于该特定联系人。
已经实现了通过activity显示和选择联系人的代码,并且在应用程序中似乎有效。

1
android.provider.ContactsContract.ContactOptionsColumns具有CUSTOM_RINGTONE用于铃声URI,因此可以使用CUSTOM_RINGTONE为所选铃声更新所选联系人。 - skyman
2个回答

10

您可以使用ContactsContract.Contacts,该类具有用于此目的的列CUSTOM_RINGTONE(这是一个可读/可写列!)。

Uri contactUri;
ContentValues values = new ContentValues();
values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, 
    newRingtoneUri.toString());
context.getContentResolver().update(contactUri, values, where, args);

此外,您可能会发现这个讨论很有用(代码取自那里)。


谢谢,这是一次非常有帮助的讨论。很多开发者对Google缺乏文档和示例感到沮丧。我会尝试使用您粘贴的代码。希望能够在1.5->2.1上工作...目前应用程序正在使用的当前代码仅适用于1.5和1.6,而不适用于2.0或2.1...例如,它适用于Magic、Hero和G1,但不适用于DROID或Nexus。 - Vidar Vestnes
Vidar Vestnes,您能否发布最终的代码来将铃声设置为联系人呢?我不确定如何获取和提供音乐文件的URL到“ContentValues”。 - Sourav301
我的铃声扩展名应该始终为ogg,还是也可以使用mp3扩展名? - Joy
你是怎么获得你的联系人URI的? - Joy

1

我知道这个回复很晚,但是我在这里发帖是因为上面的那个对我没有用。

ContentValues values = new ContentValues();

    ContentResolver resolver = getContentResolver();

    File file = new File(Environment.getExternalStorageDirectory() + "/Test/ArjunMovieTelugu.mp3");
    if(file.exists()) {

        Uri oldUri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
        resolver.delete(oldUri, MediaStore.MediaColumns.DATA + "=\"" + file.getAbsolutePath() + "\"", null);


        String contact_number = "CONTACT_NUMBER";
        Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, contact_number);

        // The columns used for `Contacts.getLookupUri`
        String[] projection = new String[]{
                ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY
        };

        Cursor data = getContentResolver().query(lookupUri, projection, null, null, null);

        if (data != null && data.moveToFirst()) {
            data.moveToFirst();
            // Get the contact lookup Uri
            long contactId = data.getLong(0);
            String lookupKey = data.getString(1);
            Uri contactUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);

            values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, "Beautiful");
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

            Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
            Uri newUri = resolver.insert(uri, values);

            if(newUri != null){
                String uriString = newUri.toString();
                values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, uriString);
                Log.e("Uri String for " + ContactsContract.Contacts.CONTENT_URI, uriString);
                long updated = resolver.update(contactUri, values,null, null);

                Toast.makeText(RingtoneChange.this, "Updated : " + updated, Toast.LENGTH_LONG).show();
            }

            data.close();
        }


    } else {
        Toast.makeText(RingtoneChange.this, "File does not exist", Toast.LENGTH_LONG).show();
    }

注意:我们必须为Marshmallow添加运行时权限。
int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;

private  boolean checkAndRequestPermissions() {
int readExternal = ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE);
int writeExternal = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
int readContacts = ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS);
int writeContacts = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_CONTACTS);

List<String> listPermissionsNeeded = new ArrayList<>();

if (readExternal != PackageManager.PERMISSION_GRANTED) {
    listPermissionsNeeded.add(android.Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (writeExternal != PackageManager.PERMISSION_GRANTED) {
    listPermissionsNeeded.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (readContacts != PackageManager.PERMISSION_GRANTED) {
    listPermissionsNeeded.add(android.Manifest.permission.READ_CONTACTS);

}
if (writeContacts != PackageManager.PERMISSION_GRANTED) {
    listPermissionsNeeded.add(android.Manifest.permission.WRITE_CONTACTS);

}

if (!listPermissionsNeeded.isEmpty()){
    ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray
            (new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
    return false;
}
return true;
}

同时在Manifest文件中加入以下权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接