如何为特定联系人设置自定义铃声?

How to set custom ringtone for a specific contact?

我想为特定联系人设置铃声。我知道如何联系并为 phone 设置铃声,但如何为特定联系人设置自定义铃声?

我的场景

  1. 我点击按钮并激活intent(RingtoneManager.ACTION_RINGTONE_PICKER)
    铃声选择器 activity

  2. 选择我的铃声后我返回 ContactDetailActivity path

  3. 结束,我使用path为特定联系人设置自定义铃声

这里是点击事件更改为铃声选择器:

ic_ringtone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (Settings.System.canWrite(ContactDetailActivity.this)) {
                        Intent intent=new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
                        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, ringtone);
                        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, ringtone);
                        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI, ringtone);
                        startActivityForResult(intent , 24);
                    }else {
                        Toast.makeText(ContactDetailActivity.this, "Vui lòng cấp quyền để đặt nhạc chuông"
                                , Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                }
            }
        });

,这是我的添加铃声功能

 public void addRingtone(String path){
//Phone is phone number of contact that I got
            final Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, phone);
            final String []projection = new String[] {ContactsContract.Contacts._ID
                    , ContactsContract.Contacts.LOOKUP_KEY};
            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 = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
                if (contactUri == null) {
                    // Invalid arguments
                    return;
                }

                // Apply the custom ringtone
                final ContentValues values = new ContentValues(1);
                values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, path);
                getContentResolver().update(contactUri, values, null, null);

            } finally {
                // Don't forget to close your Cursor
                data.close();
            }
        }

这里是 onActivityResult:

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 24:
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (Settings.System.canWrite(ContactDetailActivity.this)) {
                            ringtone = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
                            addRingtone(ringtone.getPath());
//                            Toast.makeText(ContactDetailActivity.this, ringtone.getPath() + " " + phone + " " + id, Toast.LENGTH_SHORT).show();
                        }else {
                            Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }
                    }

                    break;

                default:
                    break;
            }
        }
    }

抱歉我的英语不好。 我为此花了 3 天时间,实际上我想修复它。欢迎任何帮助或建议。 非常感谢

是啊,最后我修好了,问题出在输入参数 addRingtone(ringtone.getPath()); 改成 addRingtone(ringtone.toString()); 就可以了