Search code examples
javaandroidandroid-intentringtone

How to set custom ringtone for a specific contact?


I want to set a ringtone for a specific contact. I know how to get contact and set a ringtone for phone but how I can set custom ringtone for a specific contact ?

My Scenario:

  1. I click button and active intent(RingtoneManager.ACTION_RINGTONE_PICKER) to go to
    ringtone picker activity

  2. After choose my ringtone I come back to ContactDetailActivity with a path

  3. End, I use the path to set custom ringtone for a specific contact

Here is click event to change to ringtone picker:

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);
                    }
                }
            }
        });

, here is my add ringtone function

 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();
            }
        }

And here is 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;
            }
        }
    }

Sorry about my bad english. I spend 3 days for this and I actually want to fix it. Any help or suggestion is welcome. Thanks you so much


Solution

  • Yeah, finally I fixed it, The problem is from the input parameter of addRingtone(ringtone.getPath()); change it to addRingtone(ringtone.toString()); and it will work