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

时间:2019-06-15 17:12:07

标签: java android android-intent ringtone

我想为特定联系人设置铃声。我知道如何获取联系并为手机设置铃声,但是如何为特定联系人设置自定义铃声?

我的场景

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

  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天的时间,实际上我想修复它。欢迎任何帮助或建议。 非常感谢

1 个答案:

答案 0 :(得分:0)

是的,最后我修复了它,问题是从addRingtone(ringtone.getPath());的输入参数将其更改为addRingtone(ringtone.toString());,它将起作用