设置为联系人铃声?安卓 [英] Set as Contact Ringtone? Android

查看:217
本文介绍了设置为联系人铃声?安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何添加设置为联系人铃声功能。我已经知道如何设置默认铃声,但我想不出如何设置为联系人铃声。 我的地方,我选择接触的部分,但我不知道如何分配铃声的接触。 这部分是缠着我,我似乎无法找到已经提出这个话题,问题的答案。 这是我的code到目前为止:

I am trying to learn how to add set as contact ringtone feature. I already know how to set default ringtone but I can't figure how to set as contact ringtone. I got to the part where I choose contact, but I don't know how to assign ringtone to that contact. That part is bugging me and I can't seem to find answer in questions that were already asked on this topic. Here is my code so far:

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

    }




});


}

编辑为赏金:我想知道,如果有人可以告诉我怎么做,我试着用codeS在其他问题中,但我无法将它们应用到我的code。我可以复制文件,但如何取得联系,并指定铃声的接触?

Edit for bounty: I am wondering if someone can show me how to do so, I tried with codes found in other questions but I couldn't apply them to my code. I can copy file but how to get contact and assign ringtone to that contact?

推荐答案

从<一个href="http://stackoverflow.com/questions/23404214/android-set-custom-ringtone-to-specific-contact-number">set自定义铃声到特定联系人的号码

Android有这一个专栏: ContactsContract.CUSTOM_RINGTONE

Android has a special column for this: ContactsContract.CUSTOM_RINGTONE.

所以,你可以使用 ContactsContract.Contacts.getLookupUri 来让你的联系人的乌里,即$ P $后ptty的多少,所有剩下的就是调用 ContentResolver.update

So, you could use ContactsContract.Contacts.getLookupUri to get your contact's Uri, after that pretty much all that's left is to call ContentResolver.update.

下面是查找联系人通过自己的手机号码,然后应用自定义铃声的例子:

Here's an example of looking up a contact by their phone number, then applying a custom ringtone:

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

此外,你需要同时添加的权限读取和写入联系人:

Also, you'll need to add both permissions to read and write contacts:

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

要扩展这个有点,以及如何将其修改为您的需要,改变电话号码 012-345-6789 在这行你正在寻找一个

To extend a bit on this, and how to modify it to your need, change phone number 012-345-6789 in this line to the one you are looking for

// The Uri used to look up a contact by phone number
final Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, "012-345-6789");

和设置您的默认CUSTOM_RINGTONE在手机ContactsContract。有另一个类似的,这里的选择: 设置联系人自定义铃声,怎么样?

And set your default CUSTOM_RINGTONE in your phone ContactsContract. There is another, similar, option here: Setting contact custom ringtone, how?

这篇关于设置为联系人铃声?安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆