通过编程方式为android中的联系人分配铃声

通过编程方式为android中的联系人分配铃声,android,Android,如果我试图将sd卡中的铃声分配给特定联系人。它工作得很好,但问题是铃声分配给所有联系人,而不是特定联系人 这是我的密码:- ((ListView) v.findViewById(R.id.contact_list)).setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?>

如果我试图将sd卡中的铃声分配给特定联系人。它工作得很好,但问题是铃声分配给所有联系人,而不是特定联系人

这是我的密码:-

((ListView) v.findViewById(R.id.contact_list)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ContactModel contactModel = (ContactModel) adapterView.getItemAtPosition(i);

                AudioModel audioModel = (AudioModel) getArguments().getSerializable("AudioModel");

                ContentValues values = new ContentValues();
                String username = contactModel.getName();

                Log.e("SYNC", "setting ringtone for " + username);
                ContentResolver resolver = getActivity().getContentResolver();

                File root = Environment.getExternalStorageDirectory();

                Log.e("test", "ringtone checkpoint name here: beautiful ");

                File file = new File(audioModel.getPath());
                if(file.exists()) {

                    Log.e("test", "ringtone checkpoint if file exists");

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

                    Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactModel.getContact_id());

                    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);
                    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(getActivity(), "Updated : " + updated, Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getActivity(), "File does not exist", Toast.LENGTH_LONG).show();
                }

            }
        });
但是当我使用这个代码时,它不起作用

Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactModel.getContact_id());
resolver.update(contactUri, values,null, null);

您的更新中没有任何where子句,因此它将更新所有联系人。这就是最后两个参数——where子句及其值数组

根据这个问题,我实现了如何为联系人设置铃声,并发布了答案

你能告诉我在where子句中添加什么吗
Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactModel.getContact_id());
resolver.update(contactUri, values,null, null);