设置为联系人铃声?Android

4

我正在尝试学习如何添加“设为联系人铃声”的功能。我已经知道如何设置默认铃声,但是我不知道如何将其设置为联系人铃声。 我到了选择联系人的部分,但是我不知道如何为该联系人分配铃声。 这个部分让我很困扰,我似乎找不到关于这个主题已经提出的问题的答案。 以下是我目前的代码:

static public final int CONTACT_CHOOSER_ACTIVITY_CODE = 73729;
private File csound;
private final File rpath = new File(Environment.getExternalStorageDirectory() + "/Ringtone sounds/Ringtones");


    @Override
    public void onClick(View v) {
        setContRing();

    }

    private void setContRing() {
        Boolean success = false;
        csound = new File(rpath, FNAME);rpath.mkdirs();
        if (!csound.exists()) {




            try {
                InputStream in = getResources().openRawResource(FPATH);
                FileOutputStream out = new FileOutputStream(csound.getPath());
                byte[] buff = new byte[1024];
                int read = 0;

                try {
                    while ((read = in.read(buff)) > 0) {
                        out.write(buff, 0, read);
                    }
                } finally {
                    in.close();

                    out.close();
                }
            } catch (Exception e) {
                success = false;

            }
        } else {
            success = true;
            setContRingtone();

        }

        if (!success) { 
           setContRingtone();


        }
    }

    private void setContRingtone() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        startActivityForResult(intent, CONTACT_CHOOSER_ACTIVITY_CODE);

    }




});


}

编辑赏金: 我想知道有没有人能够向我展示如何做到这一点,我试着使用在其他问题中找到的代码,但我无法将它们应用到我的代码中。我可以复制文件,但如何获取联系人并为该联系人分配铃声呢?


您可能会发现以下链接有所帮助: https://dev59.com/mkzSa4cB1Zd3GeqPnYvnhttps://dev59.com/H2zXa4cB1Zd3GeqPZvvI - JiTHiN
1个回答

1

设置特定联系人的自定义铃声 开始

安卓系统有一个专门用于此功能的列:ContactsContract.CUSTOM_RINGTONE

因此,您可以使用ContactsContract.Contacts.getLookupUri获取联系人的Uri,然后基本上只需要调用ContentResolver.update即可。

以下是通过电话号码查找联系人并应用自定义铃声的示例:

import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;

// The Uri used to look up a contact by phone number
final Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, "012-345-6789");
// The columns used for `Contacts.getLookupUri`
final String[] projection = new String[] {
    Contacts._ID, Contacts.LOOKUP_KEY
};
// Build your Cursor
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 = Contacts.getLookupUri(contactId, lookupKey);
    if (contactUri == null) {
        // Invalid arguments
        return;
    }

    // Get the path of ringtone you'd like to use
    final String storage = Environment.getExternalStorageDirectory().getPath();
    final File file = new File(storage + "/AudioRecorder", "hello.mp4");
    final String value = Uri.fromFile(file).toString();

    // Apply the custom ringtone
    final ContentValues values = new ContentValues(1);
    values.put(Contacts.CUSTOM_RINGTONE, value);
    getContentResolver().update(contactUri, values, null, null);
} finally {
    // Don't forget to close your Cursor
    data.close();
}

此外,您需要添加读取和写入联系人的权限:
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

为了更详细地说明并将其修改以满足您的需求,请将此行中的电话号码 012-345-6789 更改为您要查找的电话号码。
// The Uri used to look up a contact by phone number
final Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, "012-345-6789");

在您的手机ContactsContract中设置默认的CUSTOM_RINGTONE。这里还有另一个类似的选项: 如何设置联系人自定义铃声?

那不是问题,问题在于将其应用到我的现有代码。 - Slim C.
@Sabroan,你提供的代码并没有做太多事情。看起来你只是获取了一个文件并启动了一个意图。为什么不修改已经工作的代码以满足你的需求呢?这甚至不是我的答案,我只是从另一个问题中转载过来的,但它似乎比从头开始更容易。 - zgc7009

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