如何在 Android 上为单个联系人设置铃声?

发布于 2024-08-25 04:28:40 字数 253 浏览 9 评论 0原文

如何仅将铃声应用于选定的联系人?

我找到了一种设置适用于所有联系人的默认铃声的方法,但这不是我的目标。

我希望应用程序有一个按钮(“将铃声应用于联系人”),单击该按钮时,会启动 activityForResult 显示手机上所有联系人的列表。选择联系人后,联系人活动将关闭并返回该联系人的 URI。然后应用程序需要将选定的铃声应用于该特定联系人。

通过活动显示和选择联系人的代码已经实现,并且似乎可以在应用程序上运行。

How can I apply a ringtone to only the selected contact?

I have found a way to set the default ringtone that applies to all contacts, but that is not my goal.

I want an application to have a button ("Apply ringtone to contact") that, when clicked, starts an activityForResult displaying a list of all contacts on the phone. When a contact is selected, the contact activity closes and returns with a URI to the contact. Then the app needs to apply the selected ringtone to that specific contact.

The code for displaying and selecting contacts by an activity is already implemented and seems to work on the app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦情居士 2024-09-01 04:28:41

您可以使用 ContactsContract.Contacts ,其中有一列 CUSTOM_RINGTONE(这是一个读/写列!)用于此目的。

Uri contactUri;
ContentValues values = new ContentValues();
values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, 
    newRingtoneUri.toString());
context.getContentResolver().update(contactUri, values, where, args);

此外,您可能会发现此讨论很有用(代码采用从那里)。

You can use ContactsContract.Contacts which has a column CUSTOM_RINGTONE (which is a read/write column!) for this purpose.

Uri contactUri;
ContentValues values = new ContentValues();
values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, 
    newRingtoneUri.toString());
context.getContentResolver().update(contactUri, values, where, args);

Furthermore, you may find this discussion useful (code taken from there).

寻梦旅人 2024-09-01 04:28:41

我知道这已经太晚了,但我在这里发帖是因为上面的一个对我不起作用

ContentValues values = new ContentValues();

    ContentResolver resolver = getContentResolver();

    File file = new File(Environment.getExternalStorageDirectory() + "/Test/ArjunMovieTelugu.mp3");
    if(file.exists()) {

        Uri oldUri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
        resolver.delete(oldUri, MediaStore.MediaColumns.DATA + "=\"" + file.getAbsolutePath() + "\"", null);


        String contact_number = "CONTACT_NUMBER";
        Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, contact_number);

        // The columns used for `Contacts.getLookupUri`
        String[] projection = new String[]{
                ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY
        };

        Cursor data = getContentResolver().query(lookupUri, projection, null, null, null);

        if (data != null && data.moveToFirst()) {
            data.moveToFirst();
            // Get the contact lookup Uri
            long contactId = data.getLong(0);
            String lookupKey = data.getString(1);
            Uri contactUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);

            values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, "Beautiful");
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

            Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
            Uri newUri = resolver.insert(uri, values);

            if(newUri != null){
                String uriString = newUri.toString();
                values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, uriString);
                Log.e("Uri String for " + ContactsContract.Contacts.CONTENT_URI, uriString);
                long updated = resolver.update(contactUri, values,null, null);

                Toast.makeText(RingtoneChange.this, "Updated : " + updated, Toast.LENGTH_LONG).show();
            }

            data.close();
        }


    } else {
        Toast.makeText(RingtoneChange.this, "File does not exist", Toast.LENGTH_LONG).show();
    }

注意:我们必须为 marsh mallow 添加运行时权限,

int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;

private  boolean checkAndRequestPermissions() {
int readExternal = ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE);
int writeExternal = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
int readContacts = ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS);
int writeContacts = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_CONTACTS);

List<String> listPermissionsNeeded = new ArrayList<>();

if (readExternal != PackageManager.PERMISSION_GRANTED) {
    listPermissionsNeeded.add(android.Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (writeExternal != PackageManager.PERMISSION_GRANTED) {
    listPermissionsNeeded.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (readContacts != PackageManager.PERMISSION_GRANTED) {
    listPermissionsNeeded.add(android.Manifest.permission.READ_CONTACTS);

}
if (writeContacts != PackageManager.PERMISSION_GRANTED) {
    listPermissionsNeeded.add(android.Manifest.permission.WRITE_CONTACTS);

}

if (!listPermissionsNeeded.isEmpty()){
    ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray
            (new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
    return false;
}
return true;
}

并将所有这些权限包含在清单文件中

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

I know this was so late, but i'm posting here because above one not worked for me

ContentValues values = new ContentValues();

    ContentResolver resolver = getContentResolver();

    File file = new File(Environment.getExternalStorageDirectory() + "/Test/ArjunMovieTelugu.mp3");
    if(file.exists()) {

        Uri oldUri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
        resolver.delete(oldUri, MediaStore.MediaColumns.DATA + "=\"" + file.getAbsolutePath() + "\"", null);


        String contact_number = "CONTACT_NUMBER";
        Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, contact_number);

        // The columns used for `Contacts.getLookupUri`
        String[] projection = new String[]{
                ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY
        };

        Cursor data = getContentResolver().query(lookupUri, projection, null, null, null);

        if (data != null && data.moveToFirst()) {
            data.moveToFirst();
            // Get the contact lookup Uri
            long contactId = data.getLong(0);
            String lookupKey = data.getString(1);
            Uri contactUri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);

            values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
            values.put(MediaStore.MediaColumns.TITLE, "Beautiful");
            values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
            values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

            Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
            Uri newUri = resolver.insert(uri, values);

            if(newUri != null){
                String uriString = newUri.toString();
                values.put(ContactsContract.Contacts.CUSTOM_RINGTONE, uriString);
                Log.e("Uri String for " + ContactsContract.Contacts.CONTENT_URI, uriString);
                long updated = resolver.update(contactUri, values,null, null);

                Toast.makeText(RingtoneChange.this, "Updated : " + updated, Toast.LENGTH_LONG).show();
            }

            data.close();
        }


    } else {
        Toast.makeText(RingtoneChange.this, "File does not exist", Toast.LENGTH_LONG).show();
    }

Note: We have to add runtime permissions for marsh mallow as

int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;

private  boolean checkAndRequestPermissions() {
int readExternal = ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE);
int writeExternal = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
int readContacts = ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS);
int writeContacts = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_CONTACTS);

List<String> listPermissionsNeeded = new ArrayList<>();

if (readExternal != PackageManager.PERMISSION_GRANTED) {
    listPermissionsNeeded.add(android.Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (writeExternal != PackageManager.PERMISSION_GRANTED) {
    listPermissionsNeeded.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (readContacts != PackageManager.PERMISSION_GRANTED) {
    listPermissionsNeeded.add(android.Manifest.permission.READ_CONTACTS);

}
if (writeContacts != PackageManager.PERMISSION_GRANTED) {
    listPermissionsNeeded.add(android.Manifest.permission.WRITE_CONTACTS);

}

if (!listPermissionsNeeded.isEmpty()){
    ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray
            (new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
    return false;
}
return true;
}

and also include all these permissions in Manifest file as

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文