设为联系铃声? Android的

时间:2014-05-01 15:32:13

标签: android contact ringtone

我正在尝试学习如何添加设置为联系人铃声功能。我已经知道如何设置默认铃声,但我无法设置如何设置为联系人铃声。 我到了我选择联系的部分,但我不知道如何为该联系人分配铃声。 那部分是困扰我的,我似乎无法在已经就此主题提出的问题中找到答案。 到目前为止,这是我的代码:

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

    }




});


}

编辑赏金:我想知道是否有人可以告诉我该怎么做,我尝试了其他问题中找到的代码,但我无法将它们应用到我的代码中。我可以复制文件,但如何联系并为该联系人分配铃声?

1 个答案:

答案 0 :(得分:-1)

来自set custom ringtone to specific contact number

Android有一个专栏: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。这里有另一个类似的选择: Setting contact custom ringtone, how?